请问如何在Java中访问Mysql数据库_____一段小程序

futurelight 2003-03-21 10:47:00
import java.sql.*;


public class ConnectMySql {
public static void main(String args[]) {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/huotw";
String user = "huotw";
String password = "810120";

try {
Class.forName(driver).newInstance();
}
catch(Exception e) {
System.out.println("无法加载驱动程序:" + driver);
}

try {
Connection con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("数据库连接成功!");
con.close();
}
catch(SQLException ee) {
System.out.println("数据库连接失败!");
}
}
}

为什么总是连接失败呀?请各位帮忙分析一下!
...全文
60 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
geyf 2003-03-21
  • 打赏
  • 举报
回复
对了,你的数据库里有名为huotw的数据库吗?
没有的话先建一个,
jiaojian77 2003-03-21
  • 打赏
  • 举报
回复
getConnection(url,user,password)用法有问题,
应该这样
getConnection(url+"?"+user+";"+password)
或者getConnection(url)也可以。
futurelight 2003-03-21
  • 打赏
  • 举报
回复
mysql以指向这个端口,是不是访问http://localhost:3306地址会有什么页面出现呀
geyf 2003-03-21
  • 打赏
  • 举报
回复
你的mysql的3306端口打开了吗,默认是不打开的。
show me-->my.ini setup
futurelight 2003-03-21
  • 打赏
  • 举报
回复
通过一个Localhost怎么访问Mysql呀,好像要配置这个对吧
futurelight 2003-03-21
  • 打赏
  • 举报
回复
mysql是好的,我可以用,就是Jdbc的驱动程序无法加载
geyf 2003-03-21
  • 打赏
  • 举报
回复
mysql配置好了吗?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧