JDBC-ODBC橋連接數(shù)據(jù)庫
不足:需要在客戶端安裝ODBC驅(qū)動程序,ODBC驅(qū)動程序還需要具有客戶端的控制權(quán)限。
方法:
1.創(chuàng)建數(shù)據(jù)源
2.裝載驅(qū)動并與DBMS建立連接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnectio("jdbc:odbc:jia","sa","123");
3.查詢
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
4.更新
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(stmt.executeUpdate(sql)<=0){return false;}
else{return true;}
5.讀取數(shù)據(jù)
Statement 接口提供了3種執(zhí)行SQL語句的方法:
qexecuteQuery()
qexecuteUpdate()
qexecute()
6.tTransaction
Connection con=DriverManager.getConnectio("jdbc:odbc:jia","sa","123");
con.setAutoCommit(false);//關(guān)閉自動提交模式
Statement stmt = con.createStatement();
stmt.qexecute(sql);
stmt.qexecute(sql);
stmt.qexecute(sql);
con.commit(); //提交
con.setAutoCommit(true);//開啟自動提交模式
con.rollback(); //回滾
7.關(guān)閉連接對象
con.close();
con.isClosed();
JDBC連接數(shù)據(jù)庫
方法:
1.Oracle8/8i/9i數(shù)據(jù)庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為數(shù)據(jù)庫SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2.DB2數(shù)據(jù)庫
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample為你的數(shù)據(jù)庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
3.Sql Server7.0/2000數(shù)據(jù)庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb為數(shù)據(jù)庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4.Sybase數(shù)據(jù)庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的數(shù)據(jù)庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5.Informix數(shù)據(jù)庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword"; //myDB為數(shù)據(jù)庫名
Connection conn= DriverManager.getConnection(url);
6.MySQL數(shù)據(jù)庫
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB為數(shù)據(jù)庫名
Connection conn= DriverManager.getConnection(url);
7.PostgreSQL數(shù)據(jù)庫
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB為數(shù)據(jù)庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
8.access數(shù)據(jù)庫直連用ODBC的
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;
posted on 2010-02-23 10:33
junly 閱讀(325)
評論(0) 編輯 收藏 所屬分類:
jdbc/jndi