<%@ page language=”java” contentType=”text/html; charset=gbk”
pageEncoding=”gbk”%>
<%@ page import=”java.sql.*” %>
<%
Class.forName(“com.mysql.jdbc.Driver”);
String url = “jdbc:mysql://localhost?user=root&password=root”;
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
String sql = “SELECT id,cont from article”;
ResultSet rs = stmt.executeQuery(sql);//就是这一句话的错误但是错在那里呢
%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gbk”>
<title>Show Article Tree</title>
</head>
<body>
<table border = “1”>
<%
while(rs.next()){
%>
<tr>
<td><%= rs.getInt(“id”) %></td>
<td><%= rs.getString(“cont”) %></td>
</tr>
<%
}
%>
</table>
</body>
<%
con.close();
%>
</html>
运行发生错误:
org.apache.jasper.JasperException: No database selected
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
javax.servlet.ServletException: No database selected
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp.ShowArticle_jsp._jspService(ShowArticle_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.sql.SQLException: No database selected
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
com.mysql.jdbc.Connection.execSQL(Connection.java:3172)
com.mysql.jdbc.Statement.executeQuery(Statement.java:1197)
org.apache.jsp.ShowArticle_jsp._jspService(ShowArticle_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)