Java Sky of Zhao
受挫一次,对生活的理解加深一层;失误一次对人生的感悟增添一阶;不幸一次,对世界的认识成熟一级;磨难一次,对成功的内涵透彻一遍!
posts - 5,comments - 4,trackbacks - 0
 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-08 07:46 SINOJAVA 阅读(1154) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: