新手請教一下mysql資料連線出現的問題

VCrazy發表於2005-04-11
新學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)
.......

相關文章