日期:2014-05-20  浏览次数:20769 次

Spring 中使用JDBC

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at COM.EDS.DAO.Basedao.getConn(Basedao.java:23)
at COM.EDS.DAO.Basedao.getPersManagement(Basedao.java:77)
at COM.EDS.DAO.IMP.OrderDetailsDaoImp.getODById(OrderDetailsDaoImp.java:50)
at COM.EDS.TEXT.MyText.main(MyText.java:13)
java.lang.NullPointerException
java.lang.NullPointerException
at COM.EDS.DAO.Basedao.getPersManagement(Basedao.java:77)
at COM.EDS.DAO.IMP.OrderDetailsDaoImp.getODById(OrderDetailsDaoImp.java:50)
at COM.EDS.TEXT.MyText.main(MyText.java:13)
Exception in thread "main" java.lang.NullPointerException
at COM.EDS.DAO.IMP.OrderDetailsDaoImp.getODById(OrderDetailsDaoImp.java:51)
at COM.EDS.TEXT.MyText.main(MyText.java:13)


以上是异常信息,
我想使用Spring作为中间框架 底层使用JDBC,但是运行时报错。

package COM.EDS.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
 

public class Basedao {
public static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=DB_ESH";
public static String USER = "sa";
public static String PWD = "1";

// 连接字符串
public Connection getConn() {

Connection conn = null;
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,USER,PWD);

} catch (ClassNotFoundException e) {
System.out.println("basedao.java (ClassNotFoundException)");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("basedao.java (SQLException)");
e.printStackTrace();
}
return conn;

}

/**
* 执行跟新语句 增 删 改

* @param sql
* @param parms
* @return
*/
public int ExecuteUpdate(String sql, Object... parms) {
Connection conn = getConn();
PreparedStatement stmt = null;
int cnt = 0;
try {
stmt = conn.prepareStatement(sql);
for (int i = 0; i < parms.length; i++) {
stmt.setObject(i + 1, parms[i]);
}
cnt = stmt.executeUpdate();

} catch (SQLException e) {
System.out
.println("--------basedao.java (ExecuteUpdate.SQLException)"
+ sql);
e.printStackTrace();
return 0;
} finally {
closeAll(stmt, null, conn);
}

return cnt;
}

/**
* 执行语句后返回 ResultSet

* @param sql
* 传入Sql语句
* @return ResultSet 需要拆箱
*/
public ResultSet getPersManagement(String sql) {
try {
Statement st = getConn().createStatement();
ResultSet rs = st.executeQuery(sql);
 
return rs;

} catch (Exception e) {
System.out.println("--------basedao.java (getPersManagement)"
+ sql);
System.out.println(e);
e.printStackTrace();
return null;
}

}

/**
* 关闭连接

* @param stmt
* Statement
* @param rs
* ResultSet
* @param conn