当前位置: 代码迷 >> Java Web开发 >> JSP连接数据库不成功的缘故
  详细解决方案

JSP连接数据库不成功的缘故

热度:9785   发布时间:2013-02-25 21:11:53
JSP连接数据库不成功的原因?
页面全部代码
<%
  Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  String SQLConnecturl= "jdbc:microsoft:sqlserver://localhost:1433/NetBank";
  String TradeDbsConnecturl= "jdbc:microsoft:sqlserver://localhost:1433/tradedb";
  String user="sa";
  String Tradeuser="bsm";
  String password="123456";
  Connection conn=DriverManager.getConnection(SQLConnecturl,user,password);  
  System.out.println("Connection Successful!!!");
  Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
%>
出现的错误org.apache.jasper.JasperException: An exception occurred processing JSP page /dbsconn.jsp at line 27
指向的是这句
Connection conn=DriverManager.getConnection(SQLConnecturl,user,password);  

JDBC安装了 没问题,用户和密码也没问题。 什么原因导致这个错误?




------解决方案--------------------------------------------------------
public class DBConnection
{
public Connection conn = null;
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
private static String name = "sa";
private static String pass = "aa";

//连接数据库的方法
public DBConnection()
{
try
{
System.out.println("***加载数据库驱动***");
Class.forName(driver);
System.out.println("***数据库驱动加载成功***");
System.out.println("***连接数据库***");
conn = DriverManager.getConnection(url, name, pass);
System.out.println("***数据库连接成功***");

} catch (ClassNotFoundException ce)
{
System.out.println("找不到jar驱动包或者驱动连接字符出错!");
ce.printStackTrace();
} catch (SQLException se)
{
System.out.println("SQL连接字符串出错!");
se.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}
------解决方案--------------------------------------------------------
public class DBConnection
{
public Connection conn = null;
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
private static String name = "sa";
private static String pass = "aa";

//连接数据库的方法
public DBConnection()
{
try
{
System.out.println("***加载数据库驱动***");
Class.forName(driver);
System.out.println("***数据库驱动加载成功***");
System.out.println("***连接数据库***");
conn = DriverManager.getConnection(url, name, pass);
System.out.println("***数据库连接成功***");

} catch (ClassNotFoundException ce)
{
System.out.println("找不到jar驱动包或者驱动连接字符出错!");
ce.printStackTrace();
} catch (SQLException se)
{
System.out.println("SQL连接字符串出错!");
se.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
}
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!