各位大侠帮小弟看看一个jsp文件的错误,

J2EE 码拜 9年前 (2015-05-10) 482次浏览 0个评论
 

<%@ 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)

5分
你的url好像不太对啊:jdbc:mysql://localhost?user=root&password=root 其中少了端口和数据库的名称。
试用一下这个
jdbc:mysql://localhost:3306/databasename?user=root&password=root
5分
应该是这样的吧
Class.forName(“com.mysql.jdbc.Driver”);
String url = “jdbc:mysql://127.0.0.1/databaseName,user,password”;
Connection con = DriverManager.getConnection(url);
5分
关注
5分
String url = “jdbc:mysql://localhost?user=root&password=root”;
—-此行少了指定database name, 故错误中报了"java.sql.SQLException: No database selected
No database selected

已经很清楚了,看来英文要加强

谢谢了,likgui给错分了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明各位大侠帮小弟看看一个jsp文件的错误,
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!