| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5030 人关注过本帖
标题:jsp与mysql数据库链接问题!
只看楼主 加入收藏
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
收藏
 问题点数:0 回复次数:29 
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
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 
受不鸟LZ了
你好歹也写个类稍微封装下你的数据库连接啊!!也方便调试!!
你这么一大堆全写在jsp页面里,HoHo
看得我老人家眼睛都晕了!!哈哈
写个类封装下吧,这是个小问题,你自己检查下就可以解决的,动下脑筋嘛!!


当神已无能为力,就让爷来普度众生吧!
2007-11-07 18:05
wenbin171
Rank: 1
等 级:新手上路
帖 子:119
专家分:0
注 册:2007-11-6
收藏
得分:0 
我是刚学的。不太会!你们帮帮我嘛!

-------哼!谁说不可以-------
2007-11-08 13:41
hjlwlyhjl
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-12-6
收藏
得分:0 

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);

以上的这些语句都会抛出异常的,你用try,catch语句捕获 一下异常试试!


2007-11-08 14:00
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
hjlwlyhjl
Rank: 1
等 级:新手上路
帖 子:71
专家分:0
注 册:2006-12-6
收藏
得分:0 
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;

这需要加上端口号的,同时要注意的是:一定要确认mysql驱动安装路径正确!

2007-11-08 15:10
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 

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:3306/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 15:47
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
快速回复:jsp与mysql数据库链接问题!
数据加载中...
 
   



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

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