1.? 加載一個對應數據庫的JDBC驅動
在建立到一個數據庫的連接之前,必須先加載這個數據庫的JDBC驅動程序,加載之后此driver會自動注冊到JDBC驅動列表中。加載一個
JDBC驅動有兩種方法。
a)? 在命令行方式下指定驅動器或者用冒號分割驅動器列表:
具體命令如下:
????? C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject
b)第二種方法,在程序中調用Class.forName()方法。推薦使用。。。。
????????????? try
{
?? String driverName = “com.imaginary.sql.msql.MsqlDriver”;
?? Class.forName(driverName).newInstance();
}
????????????? Catch(ClassNotFoundException e1)
???????????????? {
??????????? //catch could not find database driver exception.
? }
2.連接到數據庫。
根據您后臺待連接的數據庫不同,而有小小的差別。
a)?????? 連接到Oracle數據庫。
Connection connection = null ;
try
{
? //load the jdbc driver ;
? String driverName = “oracle.jdbc.driver.OracleDriver”;
? Class.forName(driverName).newInstance();
//create a connection to the database;
? String serverName = “127.0.0.1”;
? String serverPort = “1521”;
? String serverID = “datebase1”
? String userName = “hello”;
? String userPsw = “world”;
? String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID ;
? Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
? //catch could not find database driver exception.
}
catch(SQLException e2)
{
? //catch could not connect to the database exception.
}
b)????? 連接到一個SQL Server數據庫。
?? Connection connection = null ;
try
{
? //load the jdbc driver ;
? String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
? Class.forName(driverName).newInstance();
? //create a connection to the database;
? String serverName = “127.0.0.1”;
? String serverPort = “1433”;
? String serverID =? serverName + serverPort ;
? String userName = “hello”;
? String userPsw = “world”;
? String url = “jdbc:JSQLConnect ://” + serverID ;
? Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
? //catch could not find database driver exception.
}
catch(SQLException e2)
{
? //catch could not connect to the database exception.
}
c)????? 連接到一個MySQL數據庫上。。。。
????????? Connection connection = null ;
try
{
? //load the jdbc driver ;
? String driverName = “org.gjt.mm.mysql.Driver”;
? Class.forName(driverName).newInstance();
? ? //create a connection to the database;
? String serverName = “127.0.0.1”;
? String serverID =? “database”;
? String userName = “hello”;
? String userPsw = “world”;
? String url = “jdbc:mysql ://” + serverName + “/” + serverID ;
? Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
? //catch could not find database driver exception.
}
catch(SQLException e2)
{
? //catch could not connect to the database exception.
}
綜合上面的三種數據庫連接方式 , 其實大同小異。由于訪問不同的數據庫和所使用的數據庫驅動程序不同,所以導致代碼表面上有小小
不同,但透過表面看來,內部都是
1.? 加載一個特定的數據庫JDBC驅動。
2.? 連接到一個數據庫。
3.? 之后,就可以對一個特定的數據庫進行特定的操作了。
?
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、Sql Server7.0/2000數據庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為數據庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、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);
5、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);
6、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);
7、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);