[轉]
JAVA與數據庫連接方法(一)
?
?
用JAVA連接數據庫主要有兩種方式,一是用JDBC-ODBC橋來連接,二是用相關廠商提供的相應驅動程序來連接,首先談談第一種連接。
JDBC-ODBC橋接器是用JdbcOdbc.Class和一個用于訪問ODBC驅動程序的本地庫實現的。對于WINDOWS平臺,該本地庫是一個動態連接庫DLL
(JDBCODBC.DLL)。
由于JDBC在設計上與ODBC很接近。在內部,這個驅動程序把JDBC的方法映射到ODBC調用上,這樣,JDBC就可以和任何可用的ODBC驅動程序進行
交互了。這種橋接器的優點是,它使JDBC目前有能力訪問幾乎所有的數據庫。通行方式如圖所示:
應用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC層---數據源
具體操作方法為:
首先打開控制面板的管理工具,打開數據源(ODBC),在用戶DSN里面添加數據源(即你要連接的數據庫的名字),在這里假定連接SQL SERVER
2000的GoodsSupply數據庫。名稱填寫你要連接的數據庫的名稱(GoodsSupply),然后逐步設置,如果選用了使用SQL-SERVER密碼認證的話,
就要輸入相應的用戶名及密碼連接到數據庫。一路下一步設置完成。
在JAVA里面編寫程序進行測試,在這里我的程序是讓用戶輸入任意的表名與與列名,把該列的所有數據輸出。源代碼如下:
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"); //加載驅動
}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認證
DatabaseMetaData dmd=con.getMetaData(); //DMD為連接的相應情況
System.out.println("連接的數據庫:"+dmd.getURL());
System.out.println("驅動程序:"+dmd.getDriverName());
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結束):");
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("查詢結果為:");
do
{
result=rs.getString(cName);
//數據庫語言設置為中文,不用轉換編碼
//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與數據庫連接方法(二)
現在介紹第二種方法,用關廠商提供的相應驅動程序來連接。
這種實現方法是直接使用數據庫廠商提供的用專用的網絡協議創建的驅動程序,通過它可以直接將JDBC API調用轉換為直接網絡調用。這種調
用方式一般性能比較好,而且也是實用中最簡單的方法。因為它步需要安裝其他的庫或中間件。幾乎所有的數據庫廠商都為他們的數據庫提供
了這種數據庫提供了這種JDBC驅動程序,也可以從第三方廠商獲得這些驅動程序。
從網址http://industry.Java.sun.com/products/jdbc/drivers/可以看到所有有用的驅動程序的清單。其結果如圖所示:
應用程序---JDBC API---驅動程序---數據源
這里首先要安裝JDBC的驅動程序,推薦SP2版本的,可從微軟網站上下載
http://www.microsoft.com/downloads/details.aspx?FamilyID=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&DisplayLang=en 下載最下面的
SETUP.EXE
這個驅動程序要配合SQL SERVER2000 SP3A,相應下載URL為
http://www.microsoft.com/china/sql/downloads/sp3.asp 下載 chs_sql2ksp3.exe
如果用JAVA SDK直接編譯運行的話需要設置環境變量,將安裝好的JDBC驅動里面的LIB三個文件設置為環境變量:
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;
安裝即可用微軟的驅動程序連接數據庫了,相應代碼與前面基本相同:
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("驅動程序已加載");
//SQL SERVER的登陸方式必須為使用SQL SERVER密碼登陸認證方式
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://SERVERNAME:1433","USER","PASSWORD");
con.setCatalog("GoodsSupply");
System.out.println("OK,成功連接到數據庫");
}catch(Exception ex) {
ex.printStackTrace();
}
try
{
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結束):");
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("查詢結果為:");
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與數據庫連接方法(三)
最后給出JAVA連接其他數據庫的關鍵代碼:
1、Oracle8/8i/9i數據庫(thin模式)???
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();???
String? url="jdbc:oracle:thin:@localhost:1521:orcl";???
//orcl為數據庫的SID???
String? user="test";???
String? password="test";???
Connection? conn=? DriverManager.getConnection(url,user,password);???
?
2、DB2數據庫???
Class.forName("com.ibm.db2.jdbc.app.DB2Driver? ").newInstance();???
String? url="jdbc:db2://localhost:5000/sample";???
//sample為你的數據庫名???
String? user="admin";???
String? password="";???
Connection? conn=DriverManager.getConnection(url,user,password);????
?
3、Sybase數據庫???
Class.forName("com.sybase.jdbc.SybDriver").newInstance();???
String? url? ="? jdbc:sybase:Tds:localhost:5007/myDB";???
//myDB為你的數據庫名???
Properties? sysProps? =? System.getProperties();???
SysProps.put("user","userid");???
SysProps.put("password","user_password");???
Connection? conn=? DriverManager.getConnection(url,? SysProps);???
?
4、Informix數據庫???
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為數據庫名???
Connection? conn=? DriverManager.getConnection(url);???
?
5、MySQL數據庫???
Class.forName("org.gjt.mm.mysql.Driver").newInstance();???
String? url? ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=?
true&characterEncoding=8859_1"???
//myDB為數據庫名???
Connection? conn=? DriverManager.getConnection(url);???
?
6、PostgreSQL數據庫???
Class.forName("org.postgresql.Driver").newInstance();???
String? url? ="jdbc:postgresql://localhost/myDB"???
//myDB為數據庫名???
String? user="myuser";???
String? password="mypassword";???
Connection? conn=? DriverManager.getConnection(url,user,password);
posted on 2006-04-10 07:22
SINOJAVA 閱讀(257)
評論(0) 編輯 收藏 所屬分類:
Java VS DataBase