81,092
社区成员
发帖
与我相关
我的任务
分享
<%
//装载JDBC驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
//设置连接字符串
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB;";
//取得数据库连接
Connection conn=DriverManager.getConnection(url,"sa","123456");
Statement sm=conn.createStatement();
ResultSet rs=sm.executeQuery("select * from manager");
while(rs.next()){
String uid=rs.getString("manageID");
out.print(uid);
}
%>
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB;integratedSecurity=true;
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB;integratedSecurity=true;
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:850)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.connectDB_jsp._jspService(connectDB_jsp.java:108)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB;integratedSecurity=true;
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.connectDB_jsp._jspService(connectDB_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
package stu_DB;
import java.sql.*;
public class ConnectDB {
//获取数据库连接Connection对象
public static Connection getCon(){
Connection con=null;
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB";
String userID="sa";
String Password="123456";
con=DriverManager.getConnection(url,userID,Password);
}
catch(Exception e){
e.printStackTrace();
}
return con;
}
/*
*
*
*/
public static void main(String[] args){
Statement stmt=null;
ResultSet rst=null;
ConnectDB cdb=new ConnectDB();
try{
stmt=(cdb.getCon()).createStatement();
rst=stmt.executeQuery("select * from manager");
while(rst.next()){
System.out.print("ID:"+rst.getString(1)+" ");
System.out.println("Name: "+rst.getString(2)+"; ");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at stu_DB.ConnectDB.getCon(ConnectDB.java:9)
at stu_DB.ConnectDB.main(ConnectDB.java:29)
java.lang.NullPointerException
at stu_DB.ConnectDB.main(ConnectDB.java:29)
String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=StuDB";
//改成---〉
String url="jdbc:sqlserver://localhost:1433;databaseName=StuDB";