首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

MyEclipse连接数据库出有关问题了(no suitable driver)

2013-09-11 
MyEclipse连接数据库出问题了(no suitable driver)这个是我的数据库连接代码public final static String U

MyEclipse连接数据库出问题了(no suitable driver)
这个是我的数据库连接代码
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();    }} 


[解决办法]
java程序:


import java.sql.*; 

public class JDBCTest { 

public static void main(String[] args){ 


String driver = "com.mysql.jdbc.Driver"; // 驱动程序名


String url = "jdbc:mysql://localhost/scutcs"; // URL指向要访问的数据库名scutcs

String user = "........"; // MySQL配置时的用户名
  
String password = "........"; // MySQL配置时的密码

try { 

Class.forName(driver); // 加载驱动程序


Connection conn = DriverManager.getConnection(url, user, password); // 连续数据库

if(!conn.isClosed()) 
System.out.println("Succeeded connecting to the Database!"); //验证是否连接成功


Statement statement = conn.createStatement(); // statement用来执行SQL语句


String sql = "select * from student"; // 要执行的SQL语句


ResultSet rs = statement.executeQuery(sql); // 结果集

System.out.println("-----------------------------------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------------------------------");
System.out.println(" 学号" + "\t" + " 姓名" + "\t\t" + "性别");
System.out.println("-----------------------------------------");

String name = null;

while(rs.next()) {


name = rs.getString("sname"); // 选择sname这列数据


System.out.println(rs.getString("sno") + "\t" + name + "\t" + rs.getString("sex")); // 输出结果
}

rs.close();
conn.close(); 

} catch(ClassNotFoundException e) { 


System.out.println("Sorry,can`t find the Driver!"); 
e.printStackTrace(); 


} catch(SQLException e) { 


e.printStackTrace(); 


} catch(Exception e) { 


e.printStackTrace(); 




}




[解决办法]

Java code
String url = "jdbc:sqlserver://localhost:1433;databaseName=bbs;" // 1433? SQL server 2005 don't have default port : 1433.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");Connection connection =  DriverManager.getConnection(url,username,password);
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行