标签:servlet sqlserver 用jdbc编程的运行时错误及其解决大全 myeclipse jdbc错误
1、java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
3、Invalid parameter binding(s)
6、Value can not be converted to requested type
package edu.ccniit.jspCourse.db.prim; import java.sql.DriverManager; import java.sql.SQLException; /** * 获得指定ID的图书的信息 * @author Administrator * */ public class GetBookByID { /** * @param args * @throws ClassNotFoundException * @throws SQLException */ public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //----------------------------------------------------------------------------------------- String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433"; java.sql.Connection con = DriverManager.getConnection(dbUrl,"sa","123"); //----------------------------------------------------------------------------------------- String sqlStr = "select * from jspBook where bookId=?"; java.sql.PreparedStatement pst = con.prepareStatement(sqlStr); //------------------------------------------------------------------------------------------ pst.setInt(3, 3); //------------------------------------------------------------------------------------------ java.sql.ResultSet rs = pst.executeQuery(); //------------------------------------------------------------------------------------------ while(rs.next()) { String book_id = rs.getString("bookId"); String book_name = rs.getString("book_name"); System.out.println(book_id+" "+book_name); } pst.close(); con.close(); } }
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.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 edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:23)
1.2 错误描述
错误信息:SQLServer的JDBC驱动程序类com.microsoft.jdbc.sqlserver.SQLServerDriver无法找到。
造成错误的原因:没有将SQLServer的3个驱动程序文件放到项目的构建路径中。
1.3错误解决方法
将SQLServer的三个驱动文件放到项目的构建路径中。操作步骤是:
步骤1:在项目中新建一个名称为lib的目录,操作顺序为:
鼠标右击项目名-〉新建-〉文件夹
步骤2:将数据库驱动的3个文件拷贝到lib目录下,操作顺序为
拷贝数据库取得的3个文件->选择项目中的lib目录并单击鼠标右键->选择粘贴
步骤3:鼠标右击项目名-〉选择构建路径-〉配置构建路径-〉选择库
标签项,将打开以下页面:
步骤4:点击添加按钮,并选择jspCourseExample项目下的lib目录中的3个文件,并点击确定按钮,界面如下:
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 ‘sa‘ 登录失败。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSLoginRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:26)
错误信息:用sa用户名和123密码无法登录SQLServer数据库
请输入正确的数据库密码,如果数据库没有密码或密码忘记,可以在
SQLServer的企业管理器中的安全中选项中来修改sa用户密码。
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameterIndex(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setObjectInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setInt(Unknown Source)
at edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:32)
错误信息:对SQL语句中的?指定实际数据时出错,一般是将?的序号指定错了,SQL中的?的序号从1开始。并且指定的实际数据的值得类型必须与数据库中对应的列的数据类型,并且要与pst.set×××(…)中×××指定的类型相一致。
将pst.setInt(3, 3);代码改为pst.setInt(3, 3);
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 ‘jspBook‘ 无效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:34)
错误信息:代码的34行"select * from jspBook where bookId=?"中,jspBook代表表名,即数据库中并没有名称为jspBook的表。
步骤1:一定要确保数据库的URL中一定要有databaseName参数,否则JDBC
连接的就是master库,但master库并不是我们要进行操作的数据库。
步骤2:指定好databaseName参数后,查看数据库中是否有名称为jspBook的表。此处应 将jspBook改为正确的表名,即将
String sqlStr = "select * from jspBook where bookId=?";代码改
为String sqlStr = "select * from book where bookId=?";
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]列名 ‘bookId‘ 无效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:34)
错误信息:34行代码将用"select * from book where bookId=?"查询数据库,其中bookId代表列名,但book表中并没有名称为bookId的列。
将jspBook改为正确的表名,即将
String sqlStr = "select * from book where bookId=?";代码改
为String sqlStr = "select * from book where book_Id=?";
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Value can not be converted to requested type.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseData.getInteger(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getInt(Unknown Source)
at edu.ccniit.jspCourse.db.prim.GetBookByID.main(GetBookByID.java:39)
错误信息:39行代码(int book_name = rs.getInt("book_name");)出错。出错的原因是:在book表中,book_name列的数据类型是varchar(40),即 String类型,而代码中要将数据库中String类型转换成int类型,但是在Java中String类型不能自动转换成int类型,所以出错。
记住:表中某个字段是什么类型,我们用get×××()方法获得此列的数据时,也应该用同样的类型,否则会报错。
将
int book_name = rs.getInt("book_name");代码改
为String book_name = rs.getString("book_name");
标签:servlet sqlserver 用jdbc编程的运行时错误及其解决大全 myeclipse jdbc错误
原文地址:http://blog.csdn.net/xlgen157387/article/details/39055085