新手请教一下mysql数据连接出现的问题

05-04-11 VCrazy

新学jsp,编了个JSP测试一下,MYSQL已经启动,却始终无法链接上MYSQL,不知为何?

源代码:

package com.reg;

import java.sql.*;

public class UserRegist {

private UserInfo userInfo;

private Connection con;

private PreparedStatement stmt;

public UserRegist(){

String CLASSFORNAME="org.gjt.mm.mysql.Driver";

String SERVERDB="jdbc:mysql://localhost:3306/a";

try{

Class.forName(CLASSFORNAME);

con=DriverManager.getConnection(SERVERDB,"root","");

}

catch(Exception e){

e.printStackTrace();

}

}

public void setUserInfo(UserInfo userInfo){

this.userInfo=userInfo;

}

public void regist()throws Exception{

String reg="insert into test values(?,?,?,?)";

try{

PreparedStatement stmt=con.prepareStatement(reg);

stmt.setString(1,userInfo.getUserid());

stmt.setString(2,userInfo.getPassword());

//stmt.setString(3,userInfo.getEmail());

//stmt.setInt(4,userInfo.getCityType());

stmt.executeUpdate();

}

catch(Exception e){

e.printStackTrace();

throw e;

}

finally{

stmt.close();

con.close();

}

}

}

报错:

java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is ther

e a MySQL server running on the machine/port you are trying to connect to? (java

.lang.NumberFormatException)

at org.gjt.mm.mysql.Connection.connectionInit(Unknown Source)

at org.gjt.mm.mysql.jdbc2.Connection.connectionInit(Unknown Source)

at org.gjt.mm.mysql.Driver.connect(Unknown Source)

at java.sql.DriverManager.getConnection(DriverManager.java:512)

at java.sql.DriverManager.getConnection(DriverManager.java:171)

at com.reg.UserRegist.<init>(UserRegist.java:2

at org.apache.jsp.newuser_jsp._jspService(newuser_jsp.java:92)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper

.java:324)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:2

92)

.......

leec1981
2005-04-14 16:36

你把localhost改为你机器的本地ip试试看看