下面给你一个例子看看Class.forName是干什么的,注意看注释。代码如下:
Connection connection=null;
try{
//Load the JDBC driver
String driverName=”COM.ibm.db2.jdbc.app.DB2Driver”;
Class.forName(driverName);
String serverName=”127.0.0.1” //Create connection
String portNumber=”1521”
String sid=”mydatabase”;
String url=”jdbc:db2:sample”;
String username=”username”;
String password=”password”;
Connection=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException e){
//Could not find database driver
}catch(SQLException e){
//Could not connect to the database
}
Connection connection=null;
try{
//Load the JDBC driver
String driverName=”COM.ibm.db2.jdbc.app.DB2Driver”;
Class.forName(driverName);
String serverName=”127.0.0.1” //Create connection
String portNumber=”1521”
String sid=”mydatabase”;
String url=”jdbc:db2:sample”;
String username=”username”;
String password=”password”;
Connection=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException e){
//Could not find database driver
}catch(SQLException e){
//Could not connect to the database
}
会装载COM.ibm.db2.jdbc.app.DB2Driver并初始化
DB2Driver初始化时会自动注册JDBC驱动
url=...; //some url
Connection conn=DriverManager.getConnection(url);
没有提供用户名和密码!