求助JDBC连接问题,谢谢啊.
请各位高手帮忙看看以下程序,在连接数据库时出错(sqlserver服务器已打开并存在jspdev数据库;sqlserver三个驱动程序已经加载到shop工程目录下的lib下;在D:\apache-tomcat-5.5.12\common\lib下也存在此驱动程序),源程序和错误提示如下:
谢谢!
package shop;
import java.sql.*;
public class DBConnectBean {
public static Connection getSqlserverConn()
{
Connection conn=null;
String url= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
String sqldriver= "jdbc:microsoft:sqlserver://localhost:1433 ";
try {
Class.forName(url);
conn=DriverManager.getConnection(sqldriver, "sa ", " ");
conn.setCatalog( "jspdev ");
}
catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}
}
package shop;
import java.sql.*;
public class SqlserverInit {
public static void createTable()
{
Connection con=null;
try {
con=DBConnectBean.getSqlserverConn();
Statement stmt = con.createStatement();
stmt.execute( "create table type(type_id int primary key,type_name varchar(50)) ");
stmt.execute( "create table product(prod_id int primary key,prod_name varchar(50),price float,type int) ");
con.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void insertData()
{
Connection con=null;
try{
con=DBConnectBean.getSqlserverConn();
Statement stmt=con.createStatement();
con.setAutoCommit(false);
stmt.addBatch( "insert into type values(100, '玉器系列 ') ");
stmt.addBatch( "insert into type values(200, '雕塑系列 ') ");
stmt.addBatch( "insert into product values(10001, '涴雄 ',100,100) ");
stmt.addBatch( "insert into product values(10002, '鹿 ',100,100) ");
stmt.addBatch( "insert into product values(10003, '领头羊 ',100,100) ");
stmt.addBatch( "insert into product values(10004, '熊猫 ',100,200) ");
stmt.addBatch( "insert into product values(10005, '勇攀高峰 ',100,200) ");
stmt.addBatch( "insert into product values(10006, '羚羊 ',100,200) ");
stmt.executeBatch();
con.commit();
con.setAutoCommit(true);
con.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
createTable();
insertData();
System.out.println( "数据库的初始化已经完成 ");
}
}
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at shop.DBConnectBean.getSqlserverConn(DBConnectBean.java:11)
at shop.SqlserverInit.createTable(SqlserverInit.java:9)
at shop.SqlserverInit.main(SqlserverInit.java:44)
java.lang.NullPointerException
at shop.SqlserverInit.createTable(SqlserverInit.java:10)
at shop.SqlserverInit.main(SqlserverInit.java:44)
Exception in thread "main "
[解决办法]
很明显还是没找到com.microsoft.jdbc.sqlserver.SQLServerDriver
[解决办法]
java.lang.NullPointerException
空指针,调试一下看看
[解决办法]
在DBConnectBean 里写个MAIN()调一下自己,看看返回的CONN对象
如果报异常的话说明连接数据库确实有问题
[解决办法]
没找着驱动包