这个是我的数据库连接代码
public final static String URL ="jdbc:microsoft:sqlserver://localhost:1433;databaseName=bbs;user=sa;password=1235813";
public Connection getConnection() throws SQLException
{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e){
}
conn =DriverManager.getConnection(URL);
return conn;
}
这是报错的代码,一直不明白错在哪里,开始以为是MsSqlExpress的问题,可是我没有那个服务,老师说跟这无关,恩,我用的SQL2005,所有的服务都开了,也有JDBC,所以估计就是连接数据库这段代码有错,我是菜鸟,对数据库连接不熟,所以请各位朋友帮帮忙了,困扰好长时间了
HTTP Status 500 -
--------------------------------------------
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
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: java.sql.SQLException: No suitable driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:155)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.sql.SQLException: No suitable driver
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
Dao.BasicDao.getConnection(BasicDao.java:12)
Dao.BoardDao.findBoardDao(BoardDao.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
--------------------------------------------
Apache Tomcat/6.0.20
------解决方案--------------------
我的资源里有本 myEclipse 中文教程,楼主可以去看看
------解决方案--------------------
No suitable driver
没有符合的驱动;声明的驱动有问题
------解决方案--------------------
- Java code
package com.xiao7.sql;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.DriverManager;import java.sql.ResultSet;public class MsStandardConToSSCode { private Connection con = null; private final String url = "jdbc:sqlserver://"; private final String serverName = "127.0.0.1"; private final String portNumber = "1433"; private final String databaseName = "wuhen"; private final String userName = "sa"; private final String password = "xiao7520"; // Informs the driver to use server a side-cursor, // which permits more than one active statement // on a connection. // private final String selectMethod = "cursor"; // Constructor public MsStandardConToSSCode() { } private String getConnectionUrl() { return url + serverName + ":" + portNumber + ";databasename=" + databaseName; } private java.sql.Connection getConnection() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(getConnectionUrl(), userName, password); if (con != null){ System.out.println("Connection Successful!"); } } catch (Exception e) { e.printStackTrace(); System.out.println("Error Trace in getConnection() : " + e.getMessage()); } return con; } /* * Display the driver properties, database details */ public void displayDbProperties() { DatabaseMetaData dm = null; ResultSet rs = null; try { con = this.getConnection(); if (con != null) { dm = con.getMetaData(); System.out.println("Driver Information"); System.out.println("\tDriver Name: " + dm.getDriverName()); System.out.println("\tDriver Version: " + dm.getDriverVersion()); System.out.println("\nDatabase Information "); System.out.println("\tDatabase Name: " + dm.getDatabaseProductName()); System.out.println("\tDatabase Version: " + dm.getDatabaseProductVersion()); System.out.println("Avalilable Catalogs "); rs = dm.getCatalogs(); while (rs.next()) { System.out.println("\tcatalog: " + rs.getString(1)); } rs.close(); rs = null; closeConnection(); } else System.out.println("Error: No active Connection"); } catch (Exception e) { e.printStackTrace(); } dm = null; } private void closeConnection() { try { if (con != null) con.close(); con = null; } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { MsStandardConToSSCode myDbTest = new MsStandardConToSSCode(); myDbTest.displayDbProperties(); }}