发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 CQRS 扩展性 并发编程 事件溯源 分布式 SOA

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

2005-04-11 12:05
赞助商链接

新学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)
.......

2005-04-14 16:36

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

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com