JSP网页连接数据库报错,这是什么问题?
typeExceptionreportmessageAnexceptionoccurredprocessingJSPpage/news_add_control.jspat...
type Exception report
message An exception occurred processing JSP page
/news_add_control.jsp at line 23
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 /news_add_control.jsp at line 2320: String nsummary=request.getParameter("nsummary");21: String str="select max(nid) nid from newsinfo";22: int maxid=0;23: rs=stmt.executeQuery(str);24: if(rs.next()){25: maxid=rs.getInt("nid")+1;26: } 展开
message An exception occurred processing JSP page
/news_add_control.jsp at line 23
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 /news_add_control.jsp at line 2320: String nsummary=request.getParameter("nsummary");21: String str="select max(nid) nid from newsinfo";22: int maxid=0;23: rs=stmt.executeQuery(str);24: if(rs.next()){25: maxid=rs.getInt("nid")+1;26: } 展开
2个回答
展开全部
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<html>
<body>
<table border="1" width="500" bgcolor="#6e4afr">
<tr>
<td><b> id<b> </td>
</tr>
<tr>
<td><b> name<b> </td>
</tr>
<tr>
<td><b> age<b> </td>
</tr>
<tr>
<td><b> depth <b></td>
</tr>
<%
String userName = "root";//数据库用户名
String userPasswd = "123456";//密码
String dbName = "test"; //数据库名
String tableName = "stu"; //表名
String url = "jdbc:mysql://localhost:3306/" + dbName;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, userName,userPasswd);
Statement stat = conn.createStatement();
String sql = "SELECT * FROM " + tableName;
ResultSet rs = stat.executeQuery(sql);
// 输出每一个数据值
String id, name,age, depth;
if (rs != null) { //判断以下
while (rs.next()) {
id = rs.getString("id");
name = rs.getString("name");
age = rs.getString("age");
depth = rs.getString("depth");
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=age%></td>
<td><%=depth%></td>
</tr>
<%
}
}
rs.close();//关闭结果集
stat.close();//关闭Statement
conn.close();//关闭数据库连接
%>
</table>
</body>
</html>
我的jsp连接mysql给你参考下~~注意要导入sqljdbc.jar包~~~
<html>
<body>
<table border="1" width="500" bgcolor="#6e4afr">
<tr>
<td><b> id<b> </td>
</tr>
<tr>
<td><b> name<b> </td>
</tr>
<tr>
<td><b> age<b> </td>
</tr>
<tr>
<td><b> depth <b></td>
</tr>
<%
String userName = "root";//数据库用户名
String userPasswd = "123456";//密码
String dbName = "test"; //数据库名
String tableName = "stu"; //表名
String url = "jdbc:mysql://localhost:3306/" + dbName;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, userName,userPasswd);
Statement stat = conn.createStatement();
String sql = "SELECT * FROM " + tableName;
ResultSet rs = stat.executeQuery(sql);
// 输出每一个数据值
String id, name,age, depth;
if (rs != null) { //判断以下
while (rs.next()) {
id = rs.getString("id");
name = rs.getString("name");
age = rs.getString("age");
depth = rs.getString("depth");
%>
<tr>
<td><%=id%></td>
<td><%=name%></td>
<td><%=age%></td>
<td><%=depth%></td>
</tr>
<%
}
}
rs.close();//关闭结果集
stat.close();//关闭Statement
conn.close();//关闭数据库连接
%>
</table>
</body>
</html>
我的jsp连接mysql给你参考下~~注意要导入sqljdbc.jar包~~~
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询