?JAVA與數(shù)據(jù)庫連接方法(一)
?
?
用JAVA連接數(shù)據(jù)庫主要有兩種方式,一是用JDBC-ODBC橋來連接,二是用相關(guān)廠商提供的相應(yīng)驅(qū)動程序來連接,首先談?wù)劦谝环N連接。
JDBC-ODBC橋接器是用JdbcOdbc.Class和一個用于訪問ODBC驅(qū)動程序的本地庫實現(xiàn)的。對于WINDOWS平臺,該本地庫是一個動態(tài)連接庫DLL
(JDBCODBC.DLL)。
由于JDBC在設(shè)計上與ODBC很接近。在內(nèi)部,這個驅(qū)動程序把JDBC的方法映射到ODBC調(diào)用上,這樣,JDBC就可以和任何可用的ODBC驅(qū)動程序進(jìn)行
交互了。這種橋接器的優(yōu)點是,它使JDBC目前有能力訪問幾乎所有的數(shù)據(jù)庫。通行方式如圖所示:
應(yīng)用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC層---數(shù)據(jù)源
具體操作方法為:
首先打開控制面板的管理工具,打開數(shù)據(jù)源(ODBC),在用戶DSN里面添加數(shù)據(jù)源(即你要連接的數(shù)據(jù)庫的名字),在這里假定連接SQL SERVER
2000的GoodsSupply數(shù)據(jù)庫。名稱填寫你要連接的數(shù)據(jù)庫的名稱(GoodsSupply),然后逐步設(shè)置,如果選用了使用SQL-SERVER密碼認(rèn)證的話,
就要輸入相應(yīng)的用戶名及密碼連接到數(shù)據(jù)庫。一路下一步設(shè)置完成。
在JAVA里面編寫程序進(jìn)行測試,在這里我的程序是讓用戶輸入任意的表名與與列名,把該列的所有數(shù)據(jù)輸出。源代碼如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
public class ODBCBridge {
public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加載驅(qū)動
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD"); //使用SQL-SERVER2000認(rèn)證
DatabaseMetaData dmd=con.getMetaData(); //DMD為連接的相應(yīng)情況
System.out.println("連接的數(shù)據(jù)庫:"+dmd.getURL());
System.out.println("驅(qū)動程序:"+dmd.getDriverName());
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結(jié)束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command); //執(zhí)行查詢
if(!rs.next())
System.out.println("表名或列名輸入有誤");
else {
System.out.println("查詢結(jié)果為:");
do
{
result=rs.getString(cName);
//數(shù)據(jù)庫語言設(shè)置為中文,不用轉(zhuǎn)換編碼
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}?
JAVA與數(shù)據(jù)庫連接方法(二)
現(xiàn)在介紹第二種方法,用關(guān)廠商提供的相應(yīng)驅(qū)動程序來連接。
這種實現(xiàn)方法是直接使用數(shù)據(jù)庫廠商提供的用專用的網(wǎng)絡(luò)協(xié)議創(chuàng)建的驅(qū)動程序,通過它可以直接將JDBC API調(diào)用轉(zhuǎn)換為直接網(wǎng)絡(luò)調(diào)用。這種調(diào)
用方式一般性能比較好,而且也是實用中最簡單的方法。因為它步需要安裝其他的庫或中間件。幾乎所有的數(shù)據(jù)庫廠商都為他們的數(shù)據(jù)庫提供
了這種數(shù)據(jù)庫提供了這種JDBC驅(qū)動程序,也可以從第三方廠商獲得這些驅(qū)動程序。
從網(wǎng)址http://industry.Java.sun.com/products/jdbc/drivers/可以看到所有有用的驅(qū)動程序的清單。其結(jié)果如圖所示:
應(yīng)用程序---JDBC API---驅(qū)動程序---數(shù)據(jù)源
這里首先要安裝JDBC的驅(qū)動程序,推薦SP2版本的,可從微軟網(wǎng)站上下載
http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&DisplayLang=en 下載最下面的
SETUP.EXE
這個驅(qū)動程序要配合SQL SERVER2000 SP3A,相應(yīng)下載URL為
http://www.microsoft.com/china/sql/downloads/sp3.asp 下載 chs_sql2ksp3.exe
如果用JAVA SDK直接編譯運(yùn)行的話需要設(shè)置環(huán)境變量,將安裝好的JDBC驅(qū)動里面的LIB三個文件設(shè)置為環(huán)境變量:
classpath:
D:\program files\Microsoft SQL Server\jdbc\lib\msbase.jar;
D:\program files\Microsoft SQL Server\jdbc\lib\mssqlserver.jar;
D:\program files\Microsoft SQL Server\jdbc\lib\msutil.jar;
安裝即可用微軟的驅(qū)動程序連接數(shù)據(jù)庫了,相應(yīng)代碼與前面基本相同:
import java.sql.*;
import java.io.*;
public class DBColumn {
public static void main(String[] args) {
Connection con=null;
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("驅(qū)動程序已加載");
//SQL SERVER的登陸方式必須為使用SQL SERVER密碼登陸認(rèn)證方式
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://SERVERNAME:1433","USER","PASSWORD");
con.setCatalog("GoodsSupply");
System.out.println("OK,成功連接到數(shù)據(jù)庫");
}catch(Exception ex) {
ex.printStackTrace();
}
try
{
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結(jié)束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command);
if(!rs.next())
System.out.println("表名或列名輸入有誤");
else {
System.out.println("查詢結(jié)果為:");
do
{
result=rs.getString(cName);
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
?
?????????????????????????????????????????????????????????????????JAVA與數(shù)據(jù)庫連接方法(三)
最后給出JAVA連接其他數(shù)據(jù)庫的關(guān)鍵代碼:
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、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);???
?
4、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);???
?
5、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);???
?
6、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);
posted on 2006-04-08 07:46
SINOJAVA 閱讀(1171)
評論(0) 編輯 收藏