求JAVA连接SQL数据库的代码
发布网友
发布时间:2022-04-09 10:32
我来回答
共3个回答
热心网友
时间:2022-04-09 12:01
package com.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class ConPool {
private String strDriverClass;
private String strJdbcUrl;
private String strUserName;
private String strUserPwd;
public ConPool(){
}
public Connection getMyJdbcConn()
{
Connection myConn=null;
try {
Class.forName(this.getStrDriverClass()).newInstance();
//DriverManager.registerDriver (new org.postgresql.Driver());
myConn= DriverManager.getConnection(this.getStrJdbcUrl(),this.getStrUserName(),this.getStrUserPwd());
}catch (Exception ex) {
System.out.print(ex);
}
return myConn;
}
public void closeConn(Connection myConn)
{
if (myConn!=null) {
try{
myConn.close();
myConn=null;
}catch(Exception e){}
}
}
/**
* @return the strDriverClass
*/
public String getStrDriverClass() {
return strDriverClass;
}
/**
* @param strDriverClass the strDriverClass to set
*/
public void setStrDriverClass(String strDriverClass) {
this.strDriverClass = strDriverClass;
}
/**
* @return the strJdbcUrl
*/
public String getStrJdbcUrl() {
return strJdbcUrl;
}
/**
* @param strJdbcUrl the strJdbcUrl to set
*/
public void setStrJdbcUrl(String strJdbcUrl) {
this.strJdbcUrl = strJdbcUrl;
}
/**
* @return the strUserName
*/
public String getStrUserName() {
return strUserName;
}
/**
* @param strUserName the strUserName to set
*/
public void setStrUserName(String strUserName) {
this.strUserName = strUserName;
}
/**
* @return the strUserPwd
*/
public String getStrUserPwd() {
return strUserPwd;
}
/**
* @param strUserPwd the strUserPwd to set
*/
public void setStrUserPwd(String strUserPwd) {
this.strUserPwd = strUserPwd;
}
public static void main(String[] args) {
Connection crmCon=null;
ConPool cp = new ConPool();
////MySQL数据库
//cp.setStrDriverClass("com.mysql.jdbc.Driver");
//cp.setStrJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gbk");
//cp.setStrUserName("test");
//cp.setStrUserPwd("test");
//postgre数据库
cp.setStrDriverClass("org.postgresql.Driver");
cp.setStrJdbcUrl("jdbc:postgresql://127.0.0.1:5432/test");
cp.setStrUserName("test");
cp.setStrUserPwd("test");
crmCon=cp.getMyJdbcConn();
if (crmCon!=null) {
System.out.println("通过jdbc连接成功!");
}
//try {
//Statement stmtCrmUpd = crmCon.createStatement();
//
//stmtCrmUpd.addBatch("truncate table trade.sms_tmp_feild ");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (0, 'membername', '%s', '会员姓名')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (1, 'cardwh', '%s', '卡号末四位')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (2, 'transmerchant', '%s', '交易店')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (3, 'transtype', '%s', '交易类型')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (4, 'transamount', '%.2f', '交易金额')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (5, 'cardbalance', '%.2f', '卡余额')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (6, 'merchantbrand', '%s', '交易商户集团名称')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (7, 'couponname', '%s', '券名称')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (9, 'cardno', '%s', '卡号')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (10, 'cardmerchant', '%s', '卡属商户名')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (11, 'transdate', '%tm月%te日', '交易日期')");
//stmtCrmUpd.addBatch("INSERT INTO trade.sms_tmp_feild VALUES (12, 'transtime', '%tT', '交易时间')");
//
//crmCon.setAutoCommit(false);
////成功的话再提交保存当前最大ID号和失败日志
//stmtCrmUpd.executeBatch();
//crmCon.commit();
//crmCon.setAutoCommit(true);
//
//}catch (Exception ex) {
//System.out.println("数据库执行异常!");
//System.out.println(ex);
//try {
//crmCon.rollback();
//}catch (Exception e) {
//System.out.println("数据库回滚异常!");
//System.out.println(e);
//}
//}
//
//ResultSet rset = null;
//
//String sql = "select field_name,field_type from trade.sms_tmp_feild";
//Map<String,String> m = new HashMap<String,String>();
//try {
//Statement stmtCrm = crmCon.createStatement();
//rset = stmtCrm.executeQuery(sql);
//
//if (rset!=null){
//while (rset.next()){
//m.put(rset.getString("field_name"), rset.getString("field_type"));
//}
//rset.close();
//}
//rset = null;
//stmtCrm.close();
//stmtCrm = null;
//}catch (Exception ex) {
//System.out.println("数据库执行异常!");
//System.out.println(ex);
//}
//
//
//String strSql = "SELECT id,replace(replace(replace(replace(replace(replace(message_template,'tm月%te日',''),'d',''),'.2f',''),'tF',''),'tT',''),'s','') as message_template," +
//"template_feilds FROM trade.message_template";
//
//try {
//Statement stmtCrm = crmCon.createStatement();
//Statement stmtCrmUpd = crmCon.createStatement();
//rset = stmtCrm.executeQuery(strSql);
//
//if (rset!=null){
//while (rset.next()){
//if (rset.getString("template_feilds")!=null&&rset.getString("message_template")!=null){
//String[] aryFeild = rset.getString("template_feilds").split(",");
//String[] aryCon = rset.getString("message_template").split("%");
//String tid = rset.getString("id");
//
//if (aryFeild!=null&&aryFeild.length>0&&aryCon!=null&&aryCon.length>0){
//StringBuilder strB = new StringBuilder();
//StringBuilder strF = new StringBuilder();
//StringBuilder bSql = new StringBuilder();
//strB.append(aryCon[0]);
//for (int i=1;i<aryCon.length;i++){
//String strFormatCon = m.get(aryFeild[i-1]);
//if (strFormatCon.indexOf("%", 1)>0){
//strF.append(aryFeild[i-1]+",");
//}
//strF.append(aryFeild[i-1]+",");
//strB.append(strFormatCon);
//strB.append(aryCon[i]);
//}
//
//if (strF.lastIndexOf(",")==strF.length()-1){
//strF.delete(strF.length()-1, strF.length());
//}
//System.out.println("原模板内容:"+rset.getString("message_template"));
//System.out.println("新摸版内容:"+strB.toString());
//System.out.println("原模板字段:"+rset.getString("template_feilds"));
//System.out.println("新模板字段:"+strF.toString());
//
//bSql.append("update trade.message_template set message_template='"+strB.toString()+"',template_feilds='"+strF.toString()+"' where id="+tid);
//
//System.out.println("修改模板Sql:"+strF.toString());
//
//stmtCrmUpd.addBatch(bSql.toString());
//}
//}
//}
//rset.close();
//}
//rset = null;
//stmtCrm.close();
//stmtCrm = null;
//
//crmCon.setAutoCommit(false);
//stmtCrmUpd.executeBatch();
//crmCon.commit();
//crmCon.setAutoCommit(true);
//
//stmtCrmUpd.close();
//stmtCrmUpd = null;
//}catch (Exception ex) {
//System.out.println("数据库执行异常!");
//System.out.println(ex);
//try {
//crmCon.rollback();
//}catch (Exception e) {
//System.out.println("数据库回滚异常!");
//System.out.println(e);
//}
//}
cp.closeConn(crmCon);
}
}
热心网友
时间:2022-04-09 13:19
package newtest;
import java.sql.*;
public class Sql_Test {
static Connection con;
static Statement sql;
static ResultSet rs;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url="jdbc:odbc:test";
String user="username";
String password="password";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
con=DriverManager.getConnection(url,user,password);//创建Connection对象,Connection对象负责和数据源进行连接
sql=con.createStatement();//创建Statement对象,Statement对象负责执行sql语句,暂解
rs=sql.executeQuery("select * from Students");
System.out.print("学号\t"+"姓名\t"+"出生日期\t\t"+"性别\t"+"专业\n");
while(rs.next()){
System.out.print(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getDate(3)
+"\t"+rs.getString(4)+"\t"+rs.getString(5)+"\n");
//System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getDate(3)+
//"\t"+rs.getString(4)+"\t"+rs.getInt(5));
}
}catch(SQLException se){}
}
}
热心网友
时间:2022-04-09 14:54
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DButil {
private static String username="scott";
private static String password="tiger";
private static String url="jdbc:oracle:thin:@192.168.1.100:1521:ORCL";
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn(){
Connection conn=null;
try {
System.out.println("--------------");
conn=DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("没有获得连接");
}
return conn;
}
public static void free(Connection conn,PreparedStatement ps,ResultSet rs){
try{
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(conn!=null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
DButil db=new DButil();
Connection conn=DriverManager.getConnection(url, username, password);
if(conn!=null){
System.out.println("连接成功");
}
System.out.println(db.getConn().getClass().getName());
}
}追问麻烦我要的是连接SQLserver的不是oracle的
追答不是一样的吗