求助conn = DriverManager.getConnection(url, name, pwd);错误

deng231520 2018-03-24 10:19:12
本人刚学jsp,学到连接数据库,可是一直出现问题,我照着资料写的,一直提示这儿有错

大神帮忙看看,谢谢了

这是jsp链接SQL server 2008 r2 的(错误的) 我下面还有一个链接Oracle的(成功链接上了!)

这是错误信息:
type Exception report

message An exception occurred processing JSP page /jdbc_sql.jsp at line 25

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /jdbc_sql.jsp at line 25

22:
23: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
24: //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
25: conn = DriverManager.getConnection(url, name, pwd);
26: stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
27: rs = stmt.executeQuery(sql);
28: /*if(rs!=null )



下面是我的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn = null;
String sql = "select * from information";
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=users";
String name = "sa";
String pwd = "231520";
ResultSet rs = null;
Statement stmt = null;

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, name, pwd);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
/*if(rs!=null )
{
String strnum = Integer.toString(rs.getRow());
out.println(strnum);
rs.first();
}*/
while (rs.next()) {
%>
<table>
<%=rs.getString(1)%>
<%=rs.getString(2)%>
<%=rs.getString(3)%>
<%=rs.getString(4)%>
<%=rs.getString(5)%>
</table>
<%
}
%>
<%
out.print("\n\n\n数据库操作成功,恭喜你");
%>
<%
rs.close();
stmt.close();
conn.close();
%>


</body>
</html>





以下是对的(链接Oracle数据库的)代码:


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn = null;
String sql = "select * from KINGS";
String url = "jdbc:oracle:thin:@localhost:1521:Orcl";
String user = "system";
String pwd = "231520";
ResultSet rs = null;
Statement stmt = null;

Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, pwd);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
/*if(rs!=null )
{
String strnum = Integer.toString(rs.getRow());
out.println(strnum);
rs.first();
}*/
while (rs.next()) {
%>
<table>
<%=rs.getString(1)%>
<%=rs.getString(2)%>
<%=rs.getString(3)%>
<%=rs.getString(4)%>
<%=rs.getString(5)%>
</table>
<%
}
%>
<%
out.print("\n\n\n数据库操作成功,恭喜你");
%>
<%
rs.close();
stmt.close();
conn.close();
%>


</body>
</html>


...全文
1649 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
迷途的码农 2018-03-27
  • 打赏
  • 举报
回复
可能是你的驱动和你的SQLserver版本问题吧
开发者_android 2018-03-27
  • 打赏
  • 举报
回复
把异常捕获,先看这句抛的异常详情是什么。 这句有问题,也就驱动名、DB名、用户名密码和jar包的事情。都再过一遍。
deng231520 2018-03-24
  • 打赏
  • 举报
回复


这是我的驱动包和数据库
package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧