急!!!jsp联接mysql数据还是出问题各位来帮帮忙,要不我要被炒了!救救我!!!!!

chensjmail 2004-12-04 02:57:25
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/exc?user=root&useUnicode=true&characterEncoding=gb2312";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from datum";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
%>
第一个字段内容为:<font color=blue>
<%=rs.getString("id")%></font>  
第二个字段内容为:<font color=blue>
<%=rs.getString("name")%></font> 
第三个字段内容为:<font color=blue>
<%=rs.getString("age")%></font><br>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

jdk安装目录:d:\java\j2sdk1.4.2
tomcat5.0安装目录:d:\tomcat 5.0

环境变量(系统变量)配置如下:
CLASSPATH :
.;d:\java\j2sdk1.4.2.\lib\tools.jar;d:\java\j2sdk1.4.2.lid\dt.jar;d:\java\j2sdk1.4.2.\bin;d:\Tomcat 5.0\lib;d:\Tomcat 5.0\common\lib\mm.mysql-2.0.14-bin.jar;

JAVA_HOME :
d:\java\j2sdk1.4.2;

path :
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;D:\Program Files\Symantec\pcAnywhere\;"C:\Program Files\Common Files\!Sunv\TransEngine";d:\java\j2sdk1.4.2\bin;

Tomcat_home :
d:\Tomcat 5.0;

tomact5.0 server已经启用。
mysql4.1.7 winmysqladmin.exe 也已经启用,可能看到exc 数据库的表datum 的字段。

将以上的jsp代码放入以下目录:
d:\Tomcat 5.0\webapps\ROOT
文件名为:TestMysql.jsp

在IE中输入:http://localhost:8080/TestMysql.jsp


IE中的错误为:

HTTP Status 500 -

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

type Exception report

message

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

exception

javax.servlet.ServletException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.TestMysql_jsp._jspService(TestMysql_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


root cause

java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)
org.gjt.mm.mysql.Connection.connectionInit(Unknown Source)
org.gjt.mm.mysql.jdbc2.Connection.connectionInit(Unknown Source)
org.gjt.mm.mysql.Driver.connect(Unknown Source)
java.sql.DriverManager.getConnection(DriverManager.java:512)
java.sql.DriverManager.getConnection(DriverManager.java:193)
org.apache.jsp.TestMysql_jsp._jspService(TestMysql_jsp.java:45)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


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


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

Apache Tomcat/5.0.16


请大家一定要救救我:
e-mail:chensjmail@163.com
一定有回报!!!!!!!!!!!!!!!!!!!!!
...全文
103 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhongzuo1981 2004-12-04
String url ="jdbc:mysql://localhost:3306/exc?user=root&useUnicode=true&characterEncoding=gb2312";
回复
zealVampire 2004-12-04
javax.servlet.ServletException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)

如果你安装了mysql在本地, 启动了? 启动之后保证端口是3306吗》??
你用bin下面的MySQLAdmin.exe还是什么 看看数据库
回复
pennymxb 2004-12-04
连接数据库出现的错误!!
回复
jFresH_MaN 2004-12-04
你在命令行执行你的查询代码试试
mysql --user root --port 3306

use exc;
select * from datum;

回复
ddboy 2004-12-04
给你一个连接MySQL的类:
<%@page import="java.sql.*"%>
<%!
/*
该文件用于处理Mysql数据库,提供连接,执行得方法
[1]初始化类时设置Mysql参数 mysql(String db,String user,String pwd)
使用省缺用户和密码 mysql(String db)
全部省缺,省缺数据库为 mysql,用户 root 密码 ""
[2]重设数据库参数 setDatabase(String db,String user,String pwd)
[3]执行SQL命令
a、boolean execute(String sql,int type)
type参数为3时,执行查SQL查询命令,成员变量
Connection con
Statement smt
ResultSet res
均被改变
type参数为2时,执行SQL更新,删除,插入操作,成员变量
Connection con
Statement smt
被改变
b、boolean execute(String sql)
执行查SQL查询命令,成员变量
Connection con
Statement smt
ResultSet res
均被改变
[4]关闭连接
void close(Connection conn,Statement smtt,ResultSet ress)
关闭查询操作
void close(Connection conn,Statement smtt)
关闭更新操作
*/
public class mysql
{
String defaultdb="mysql";
String driver="org.gjt.mm.mysql.Driver";
String url="jdbc:mysql://localhost:3306/";
String user="root";
String pwd="";
Connection con=null;
Statement smt=null;
ResultSet res=null;
mysql(String db,String user,String pwd)
{
this.url=url+db;
this.user=user;
this.pwd=pwd;
}
mysql(String db) {this.url=this.url+db;}
mysql() {this.url=this.url+this.defaultdb;}
void load_driver()
{
try
{
Class.forName(this.driver);
}
catch(Exception E)
{
E.printStackTrace();
}
}
boolean execute(String sql,int type)
{
this.load_driver();
try
{
con=DriverManager.getConnection(this.url,this.user,this.pwd);
if(type==3){
smt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
res=smt.executeQuery(sql);
}
if(type==2){
smt=con.createStatement();
smt.executeUpdate(sql);
}
return(true);
}
catch(SQLException SE)
{
SE.printStackTrace();
return(false);
}
}
boolean execute(String sql)
{
this.load_driver();
try
{
con=DriverManager.getConnection(this.url,this.user,this.pwd);
smt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
res=smt.executeQuery(sql);
return(true);
}
catch(SQLException SE)
{
SE.printStackTrace();
return(false);
}
}
void setDatabase(String db,String user,String pwd)
{
this.url=url+db;
this.user=user;
this.pwd=pwd;
}
void close(Connection conn,Statement smtt,ResultSet ress)
{
try
{
conn.close();
smtt.close();
ress.close();
}
catch(SQLException SE)
{
SE.printStackTrace();
}
}
void close(Connection conn,Statement smtt)
{
try
{
conn.close();
smtt.close();
}
catch(SQLException SE)
{
SE.printStackTrace();
}
}
}
%>
回复
ddboy 2004-12-04
可能没有安装好Jdbc-mysql 的驱动
而且String url ="jdbc:mysql://localhost/excuser=root&useUnicode=true&characterEncoding=gb2312";
好像没必要这样写吧?

回复
chensjmail 2004-12-04
楼上的是想说什么呀?
回复
chensjmail 2004-12-04
如你有心帮我解决,请将解决的详细步骤说清楚谢谢!!解决了有高分!!
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-12-04 02:57
社区公告
暂无公告