问题描述
preparedstatement=connection.prepareStatement("select*fromclass1wherestudentname=?andsex=?");preparedstatement.setString(1,"小李");//到这里就报错了preparedstatement.setString(2,"男");这是报错的代码Exceptioninthread"main"java.lang.NoClassDefFoundError:microsoft/sql/DateTimeOffsetatcom.microsoft.sqlserver.jdbc.JavaType.<clinit>(DataTypes.java:470)atcom.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1048)atPractice8.main(Practice8.java:33)Causedby:java.lang.ClassNotFoundException:microsoft.sql.DateTimeOffsetatjava.net.URLClassLoader$1.run(URLClassLoader.java:366)atjava.net.URLClassLoader$1.run(URLClassLoader.java:355)atjava.security.AccessController.doPrivileged(NativeMethod)atjava.net.URLClassLoader.findClass(URLClassLoader.java:354)atjava.lang.ClassLoader.loadClass(ClassLoader.java:423)atsun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)atjava.lang.ClassLoader.loadClass(ClassLoader.java:356)...3more求大神
解决方案
解决方案二:
importjava.sql.*;publicclassPractice8{publicstaticvoidmain(String[]args){PreparedStatementpreparedstatement=null;Connectionconnection=null;ResultSetresultset=null;try{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//System.out.println("驱动加载成功");connection=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=grade1","sa","8729355");preparedstatement=connection.prepareStatement("select*fromclass1wherestudentname=?andsex=?");preparedstatement.setString(1,"小李");preparedstatement.setString(2,"男");resultset=preparedstatement.executeQuery();while(resultset.next()){Stringstudentname=resultset.getString(1);Stringsex=resultset.getString(2);intago=resultset.getInt(3);System.out.println(studentname+"t"+sex+"t"+ago);}}catch(Exceptione){}finally{try{if(resultset!=null){resultset.close();}if(preparedstatement!=null){preparedstatement.close();}if(connection!=null){connection.close();}}catch(Exceptione){}}}}
解决方案三:
完整代码在楼上