我要编写一个与MYSQL连接的JSP 下面是我JSP的代码
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=123456&useUnicode=true&characterEncoding=8859_1" ;
//first为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from first";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
------------------over--------------------------
这个是出错的代码
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Access denied for user 'soft'@'localhost' (using password: YES)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.first_jsp._jspService(org.apache.jsp.first_jsp:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.sql.SQLException: Access denied for user 'soft'@'localhost' (using password: YES)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
com.mysql.jdbc.Connection.<init>(Connection.java:1485)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.first_jsp._jspService(org.apache.jsp.first_jsp:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
请帮帮我,我不知道为什么提示错误,我都是按照网站上的贴子做的。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=123456&useUnicode=true&characterEncoding=8859_1" ;
//first为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from first";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
------------------over--------------------------
这个是出错的代码
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Access denied for user 'soft'@'localhost' (using password: YES)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.first_jsp._jspService(org.apache.jsp.first_jsp:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.sql.SQLException: Access denied for user 'soft'@'localhost' (using password: YES)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
com.mysql.jdbc.Connection.<init>(Connection.java:1485)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.first_jsp._jspService(org.apache.jsp.first_jsp:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
请帮帮我,我不知道为什么提示错误,我都是按照网站上的贴子做的。
这个是啥啊?
应该是这样吧Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection"jdbc:mysql://localhost:3306/databasename","root","123456");
可以grant all on *.* to soft@localhost identifed bt '123456';
flush privileges;
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.first11_jsp._jspService(org.apache.jsp.first11_jsp:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.sql.SQLException: Unknown database 'softforum'
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
com.mysql.jdbc.Connection.<init>(Connection.java:1485)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.first11_jsp._jspService(org.apache.jsp.first11_jsp:50)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)可不可以帮我解释下面这代码是什么意思啊,谢谢。
String url ="jdbc:mysql://localhost/softforum?user=root&password=123456&useUnicode=true&characterEncoding=8859_1" ;
&符号代表什么意思?
softforum后面的?号代表什么意思?
如果将localhost/softforum?改为localhost,jsp页面就提示没有合适的驱动程序.这是为什么啊
另外,我觉得你好象少写了端口号,一般默认是3306,所以,正确的写法应该是
jdbc:mysql://localhost:3306/softforum?user=root&password=123456&useUnicode=true&characterEncoding=8859_1
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=123456&useUnicode=true&characterEncoding=8859_1" ;
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from first";
ResultSet rs=stmt.executeQuery(sql);
%>后面是JSP页面代码,其中在输出的地方我用
<tr>
<td bgcolor="#FFFFFF"><div align="center"><%=rs.getString(2)%></div></td>
</tr>
提取数据库第2个字段内容,为什么显示下面这段代码?
javax.servlet.ServletException: Before start of result set
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.indexzs_jsp._jspService(org.apache.jsp.indexzs_jsp:599)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
下面就是结束数据库了。
<%
rs.close();
stmt.close();
conn.close();
%> 请高手指点谢谢...急啊~~~~~~~~~~
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost:3306/first?user=test&password=123456&useUnicode=true&characterEncoding=8859_1";
//first为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from about";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
楼上的兄弟你可能碰到的问题和我的一样,我也是整了很长时间,已经可以运行了就是上面的代码,你比着修改一下。关键是修改数据库和表。
俺是个河南人!
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);