标题:jsp与mysql数据库链接问题!
取消只看楼主
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
 问题点数:0 回复次数:16 
jsp与mysql数据库链接问题!
//这是jsp链接文件:
<%@ page contentType="text/html; charset=gb2312" %>

<%@ page language="java" %>

<%@ page import="com.mysql.jdbc.Driver" %>

<%@ page import="java.sql.*" %>

<%

//驱动程序名

String driverName="com.mysql.jdbc.Driver";

//数据库用户名

String userName="root";

//密码

String userPasswd="wenbin";

//数据库名

String dbName="first";

//表名

String tableName="about";

//联结字符串

String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection connection=DriverManager.getConnection(url);

Statement statement = connection.createStatement();

String sql="SELECT * FROM "+tableName;

ResultSet rs = statement.executeQuery(sql);

//获得数据结果集合

ResultSetMetaData rmeta = rs.getMetaData();

//确定数据集的列数,亦字段数

int numColumns=rmeta.getColumnCount();

// 输出每一个数据值

out.print("id");

out.print("|");

out.print("num");

out.print("<br>");

while(rs.next()) {

out.print(rs.getString(1)+" ");

out.print("|");

out.print(rs.getString(2));

out.print("<br>");

}

out.print("<br>");

out.print("数据库操作成功,恭喜你");

rs.close();

statement.close();

connection.close();

%>




//以上代码有错吗?运行起来总报错:


HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

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

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.10



//各位,请帮忙看看!先谢谢了!
搜索更多相关主题的帖子: 数据库 jsp mysql 链接 
2007-11-07 17:59
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
我是刚学的。不太会!你们帮帮我嘛!

-------哼!谁说不可以-------
2007-11-08 13:41
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
加上try,catch语句捕获:
错误如下:

type Exception report

message

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

exception

org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:154)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

org.apache.jasper.JasperException: Unable to load class for JSP
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:600)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.ClassNotFoundException: org.apache.jsp.test.first_jsp
    java.net.URLClassLoader$1.run(Unknown Source)
    java.security.AccessController.doPrivileged(Native Method)
    java.net.URLClassLoader.findClass(Unknown Source)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:598)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.



-------哼!谁说不可以-------
2007-11-08 14:13
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
是不是我的mysql数据库有问题?

-------哼!谁说不可以-------
2007-11-08 14:15
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
我用的端口是8080:
3306不用能。
http://localhost:8080/test.jsp

把程序都给输入了:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBConnection { private static Connection conn = null; static { try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection() { String url = "jdbc:mysql://localhost:8080/first"; String user="root"; String password="wenbin"; try { conn = DriverManager.getConnection(url,user,password); }catch(SQLException e){ e.printStackTrace(); } return conn; } public static void close() { if(conn!=null) { try { conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } public void printResult(String tbName) { PreparedStatement stm = null; String sql = "select * from "+tbName; try { stm = getConnection().prepareStatement(sql); ResultSet rss = stm.executeQuery(); while(rss.next()) { System.out.print(rss.getString(1)+"|"); System.out.print(rss.getString(2)+"|"); System.out.print(rss.getString(3)); } }catch(SQLException e){ e.printStackTrace(); }finally{ close(); } } public static void main(String[] args) { new DBConnection().printResult("about"); //你将这段代码运行看看,是不是有数据输出. //表名,数据库用户以及密码我都按照你的写的,你用改了直接运行看看 } }

-------哼!谁说不可以-------
2007-11-08 16:02
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 

对不起! 我傻掉了。忘<%%>了


-------哼!谁说不可以-------
2007-11-08 16:07
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
和以前一样,你们说是什么问题!


type Exception report

message

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

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 6 in the jsp file: /testdb3.jsp
Syntax error on token "import", assert expected
3: <html> 
4: <body> 
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;


An error occurred at line: 6 in the jsp file: /testdb3.jsp
java.sql.Connection cannot be resolved
3: <html> 
4: <body> 
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;


An error occurred at line: 7 in the jsp file: /testdb3.jsp
Syntax error on token "import", assert expected
4: <body> 
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;


An error occurred at line: 7 in the jsp file: /testdb3.jsp
java.sql.DriverManager cannot be resolved
4: <body> 
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;


An error occurred at line: 8 in the jsp file: /testdb3.jsp
Syntax error on token "import", assert expected
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 


An error occurred at line: 8 in the jsp file: /testdb3.jsp
java.sql.PreparedStatement cannot be resolved
5: <%
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 


An error occurred at line: 9 in the jsp file: /testdb3.jsp
Syntax error on token "import", assert expected
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {


An error occurred at line: 9 in the jsp file: /testdb3.jsp
java.sql.ResultSet cannot be resolved
6: import java.sql.Connection;
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {


An error occurred at line: 10 in the jsp file: /testdb3.jsp
Syntax error on token "import", assert expected
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {
13: private static Connection conn = null;


An error occurred at line: 10 in the jsp file: /testdb3.jsp
java.sql.SQLException cannot be resolved
7: import java.sql.DriverManager;
8: import java.sql.PreparedStatement;
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {
13: private static Connection conn = null;


An error occurred at line: 12 in the jsp file: /testdb3.jsp
Illegal modifier for the local class DBConnection; only abstract or final is permitted
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {
13: private static Connection conn = null;
14: 
15: static


An error occurred at line: 12 in the jsp file: /testdb3.jsp
Cannot define static initializer in inner type DBConnection
9: import java.sql.ResultSet;
10: import java.sql.SQLException;
11: 
12: public class DBConnection {
13: private static Connection conn = null;
14: 
15: static


An error occurred at line: 25 in the jsp file: /testdb3.jsp
The method getConnection cannot be declared static; static methods can only be declared in a static or top level type
22: }
23: }
24: 
25: public static Connection getConnection()
26: {
27: String url = "jdbc:mysql://localhost:8080/first";
28: String user="root";


An error occurred at line: 40 in the jsp file: /testdb3.jsp
The method close cannot be declared static; static methods can only be declared in a static or top level type
37: }
38: 
39: 
40: public static void close()
41: {
42: if(conn!=null)
43: {


An error occurred at line: 73 in the jsp file: /testdb3.jsp
The method main cannot be declared static; static methods can only be declared in a static or top level type
70: close();
71: }
72: }
73: public static void main(String[] args) {
74: new DBConnection().printResult("about");
75: //你将这段代码运行看看,是不是有数据输出.
76: //表名,数据库用户以及密码我都按照你的写的,你用改了直接运行看看


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.


-------哼!谁说不可以-------
2007-11-08 16:10
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
还是不行。我要哭了!
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DBConnection.<clinit>(DBConnection.java:14)
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBConnection.getConnection(DBConnection.java:27)
at DBConnection.printResult(DBConnection.java:54)
at DBConnection.main(DBConnection.java:69)
Exception in thread "main" java.lang.NullPointerException
at DBConnection.printResult(DBConnection.java:54)
at DBConnection.main(DBConnection.java:69)

-------哼!谁说不可以-------
2007-11-08 16:32
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 

呵,人太笨了,没办法!

-------哼!谁说不可以-------
2007-11-08 16:50
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
得分:0 
打你,乱说话!

-------哼!谁说不可以-------
2007-11-08 18:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-184307-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.200917 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved