java如何连接mysql数据库

roy1984 2005-11-01 02:03:08
import java.sql.*;
public class connectMySQL
{
public static void mian(String args[])
{
String driver="org.mm.mysql.Driver";
String url="jdbc:mysql://localhost:3360/mysql?";
String user="root";
String password="";
try
{
Class.forName(driver);
}
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("数据库连接失败!");
System.out.println(ee.getMessage());
}
}
}
这些代码有什么错误啊?
...全文
714 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
daocaoren0 2005-11-01
  • 打赏
  • 举报
回复
String url="jdbc:mysql://localhost:3360/mysql?";
你的数据库名是“mysql?”???

如果数据库名是“mysql”那是不用“?”的。
Mark_MaoHUA 2005-11-01
  • 打赏
  • 举报
回复
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
elsee 2005-11-01
  • 打赏
  • 举报
回复
端口好像不对,去掉端口号使用默认端口!
另外,是main不是mian!

String driver="com.mysql.jdbc.Driver";

在我的机子上通过,我的没mysql-jdbc是3.11。
HouJinkun2005 2005-11-01
  • 打赏
  • 举报
回复
你没有指明要连接MySql里的哪个库。

给你一个我连接的例子:
conn = DriverManager.getConnection("jdbc:mysql://localhost/EP?user=root&password=qqdiy0&useUnicode=true&characterEncoding=gbk");

EP是库名

roy1984 2005-11-01
  • 打赏
  • 举报
回复
错误是:Data source rejected establishment of connection, message from server: "Host 'b
illgates' is not allowed to connect to this MySQL server"
Press any key to continue...
roy1984 2005-11-01
  • 打赏
  • 举报
回复
还是连接不了
xieyunlong 2005-11-01
  • 打赏
  • 举报
回复
String driver="org.mm.mysql.Driver";//这句写错了。应该是com.mysql.jdbc.Driver
网络精灵 2005-11-01
  • 打赏
  • 举报
回复
有,首先驱动错了,org.gjt.mm.mysql.Driver

62,625

社区成员

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

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