package com.task22;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC {
public void getstudentById(String id){
String driverClass="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String comnectUrl="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=welcomestudent";
String userName="sa";
String userPass="123456";
Connection con=null;
Statement stmt=null;
try{
Class.forName(driverClass);
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}
try{
con=DriverManager.getConnection(comnectUrl, userName, userPass);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs=null;
String sqlStr="select * from student where id="+id;
rs=stmt.executeQuery(sqlStr);
if(rs.next()){
System.out.print(rs.getString(1)+",");
System.out.print(rs.getString(2)+",");
System.out.print(rs.getString(3)+",");
System.out.print(rs.getString(4));
}
rs.close();
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
finally{
try{
if(con!=null||!con.isClosed()){
con.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[]args){
JDBC jdbc=new JDBC();
jdbc.getstudentById(("2013154"));
}
}
下面是出现的问题提示:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at com.task22.JDBC.getstudentById(JDBC.java:17)
at com.task22.JDBC.main(JDBC.java:52)
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=welcomestudent
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.task22.JDBC.getstudentById(JDBC.java:22)
at com.task22.JDBC.main(JDBC.java:52)
java.lang.NullPointerException
at com.task22.JDBC.getstudentById(JDBC.java:41)
at com.task22.JDBC.main(JDBC.java:52)