其實(shí),只是一個(gè)API熟悉度的問題,有個(gè)同事面試新來的伙子,問的一楞楞的,自己總結(jié)一下:
1 將數(shù)據(jù)庫的JDBC驅(qū)動(dòng)加載到classpath中,在基于JAVAEE的WEB應(yīng)用實(shí)際開發(fā)過程中,通常要把目標(biāo)數(shù)據(jù)庫產(chǎn)品的JDBC驅(qū)動(dòng)復(fù)制到WEB-INF/lib下.
2 加載JDBC驅(qū)動(dòng),并將其注冊(cè)到DriverManager中,下面是一些主流數(shù)據(jù)庫的JDBC驅(qū)動(dòng)加裁注冊(cè)的代碼:
//Oracle8/8i/9iO數(shù)據(jù)庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Sql Server7.0/2000數(shù)據(jù)庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//DB2數(shù)據(jù)庫
Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//Informix數(shù)據(jù)庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
//Sybase數(shù)據(jù)庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
//MySQL數(shù)據(jù)庫
Class.forName("com.mysql.jdbc.Driver").newInstance();
//PostgreSQL數(shù)據(jù)庫
Class.forNaem("org.postgresql.Driver").newInstance();
3 建立數(shù)據(jù)庫連接,取得Connection對(duì)象.例如:
//Oracle8/8i/9i數(shù)據(jù)庫(thin模式)
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String password="tiger";
Connection conn=DriverManager.getConnection(url,user,password);
//Sql Server7.0/2000數(shù)據(jù)庫
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
//DB2數(shù)據(jù)庫
String url="jdbc:db2://localhost:5000/sample";
String user="amdin"
String password=-"";
Connection conn=DriverManager.getConnection(url,user,password);
//Informix數(shù)據(jù)庫
String url="jdbc:informix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";
Connection conn=DriverManager.getConnection(url);
//Sybase數(shù)據(jù)庫
String url="jdbc:sybase:Tds:localhost:5007/tsdata";
Properties sysProps=System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn=DriverManager.getConnection(url,SysProps);
//MySQL數(shù)據(jù)庫
String url="jdbc:mysql://localhost:3306/testDB?user=root&password=root&useUnicode=true&characterEncoding=gb2312";
Connection conn=DriverManager.getConnection(url);
//PostgreSQL數(shù)據(jù)庫
String url="jdbc:postgresql://localhost/testDB";
String user="myuser";
String password="mypassword";
Connection conn=DriverManager.getConnection(url,user,password);
4 建立Statement對(duì)象或PreparedStatement對(duì)象.例如:
//建立Statement對(duì)象
Statement stmt=conn.createStatement();
//建立ProparedStatement對(duì)象
String sql="select * from user where userName=? and password=?";
PreparedStatement pstmt=Conn.prepareStatement(sql);
pstmt.setString(1,"admin");
pstmt.setString(2,"liubin");
5 執(zhí)行SQL語句.例如:
String sql="select * from users";
ResultSet rs=stmt.executeQuery(sql);
//執(zhí)行動(dòng)態(tài)SQL查詢
ResultSet rs=pstmt.executeQuery();
//執(zhí)行insert update delete等語句,先定義sql
stmt.executeUpdate(sql);
6 訪問結(jié)果記錄集ResultSet對(duì)象。例如:
while(rs.next)
{
out.println("你的第一個(gè)字段內(nèi)容為:"+rs.getString());
out.println("你的第二個(gè)字段內(nèi)容為:"+rs.getString(2));
}
7 依次將ResultSet、Statement、PreparedStatement、Connection對(duì)象關(guān)閉,釋放所占用的資源.例如:
rs.close();
stmt.clost();
pstmt.close();
con.close();
>>>>>>>>>>>>>>后加上
MySQL:
String Driver="com.mysql.jdbc.Driver"; //驅(qū)動(dòng)程序
String URL="jdbc:mysql://localhost:3306/db_name"; //連接的URL,db_name為數(shù)據(jù)庫名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).new Instance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Microsoft SQL Server 2.0驅(qū)動(dòng)(3個(gè)jar的那個(gè)):
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; //連接SQL數(shù)據(jù)庫的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name為數(shù)據(jù)庫名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).new Instance(); //加載數(shù)據(jù)可驅(qū)動(dòng)
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Microsoft SQL Server 3.0驅(qū)動(dòng)(1個(gè)jar的那個(gè)): // 老紫竹完善
String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //連接SQL數(shù)據(jù)庫的方法
String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name"; //db_name為數(shù)據(jù)庫名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).new Instance(); //加載數(shù)據(jù)可驅(qū)動(dòng)
Connection con=DriverManager.getConnection(URL,UserName,Password); //
Sysbase:
String Driver="com.sybase.jdbc.SybDriver"; //驅(qū)動(dòng)程序
String URL="jdbc:Sysbase://localhost:5007/db_name"; //db_name為數(shù)據(jù)可名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Oracle(用thin模式):
String Driver="oracle.jdbc.driver.OracleDriver"; //連接數(shù)據(jù)庫的方法
String URL="jdbc:oracle:thin:@loaclhost:1521:orcl"; //orcl為數(shù)據(jù)庫的SID
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance(); //加載數(shù)據(jù)庫驅(qū)動(dòng)
Connection con=DriverManager.getConnection(URL,Username,Password);
PostgreSQL:
String Driver="org.postgresql.Driver"; //連接數(shù)據(jù)庫的方法
String URL="jdbc:postgresql://localhost/db_name"; //db_name為數(shù)據(jù)可名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
DB2:
String Driver="com.ibm.db2.jdbc.app.DB2.Driver"; //連接具有DB2客戶端的Provider實(shí)例
//String Driver="com.ibm.db2.jdbc.net.DB2.Driver"; //連接不具有DB2客戶端的Provider實(shí)例
String URL="jdbc:db2://localhost:5000/db_name"; //db_name為數(shù)據(jù)可名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
Informix:
String Driver="com.informix.jdbc.IfxDriver";
String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver"; //db_name為數(shù)據(jù)可名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
JDBC-ODBC:
String Driver="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:dbsource"; //dbsource為數(shù)據(jù)源名
String Username="username"; //用戶名
String Password="password"; //密碼
Class.forName(Driver).newInstance();
Connection con=DriverManager.getConnection(URL,Username,Password);
posted on 2010-08-16 13:30
Daniel 閱讀(246)
評(píng)論(0) 編輯 收藏 所屬分類:
CoreJava