当前位置: 技术问答>java相关
jdbc连mysql的怪现象
来源: 互联网 发布时间:2017-04-05
本文导语: 我用jdbc连接mysql但老出错,请大家帮忙看看,我连接mysql数据库的源码如下: String drivers="org.gjt.mm.mysql.Driver"; System.setProperty("jdbc.drivers",drivers); String sconn="jdbc:mysql://10.200.5.38:3360/mysql"; try { Connection conn=Dri...
我用jdbc连接mysql但老出错,请大家帮忙看看,我连接mysql数据库的源码如下:
String drivers="org.gjt.mm.mysql.Driver";
System.setProperty("jdbc.drivers",drivers);
String sconn="jdbc:mysql://10.200.5.38:3360/mysql";
try {
Connection conn=DriverManager.getConnection(sconn,"root","");
Statement state=conn.createStatement();
ResultSet rs=state.executeQuery("select * from db");
while (rs.next())
System.out.println(rs.getString(1)+' '+rs.getString(2));
rs.close();
state.close();
conn.close();}
catch(SQLException ex)
{System.out.println(ex.getMessage());
}
编译能通过但一运行就报如下错:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.(MysqlIO.java:114)
at org.gjt.mm.mysql.Connection.(Connection.java:229)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.main(Test.java:11)
Cannot connect to MySQL server on 10.200.5.38:3360. Is there a MySQL server running on the machine/port you are trying to connect to? (java.net.ConnectException)
我的mysql服务器应该是没问题的
String drivers="org.gjt.mm.mysql.Driver";
System.setProperty("jdbc.drivers",drivers);
String sconn="jdbc:mysql://10.200.5.38:3360/mysql";
try {
Connection conn=DriverManager.getConnection(sconn,"root","");
Statement state=conn.createStatement();
ResultSet rs=state.executeQuery("select * from db");
while (rs.next())
System.out.println(rs.getString(1)+' '+rs.getString(2));
rs.close();
state.close();
conn.close();}
catch(SQLException ex)
{System.out.println(ex.getMessage());
}
编译能通过但一运行就报如下错:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at org.gjt.mm.mysql.MysqlIO.(MysqlIO.java:114)
at org.gjt.mm.mysql.Connection.(Connection.java:229)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Test.main(Test.java:11)
Cannot connect to MySQL server on 10.200.5.38:3360. Is there a MySQL server running on the machine/port you are trying to connect to? (java.net.ConnectException)
我的mysql服务器应该是没问题的
|
端口默认是3306啊,你作了调整吗?
|
你将 String drivers="org.gjt.mm.mysql.Driver";
System.setProperty("jdbc.drivers",drivers);
改为
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNOTFountException e){}
试试
还有就是要正确设置你的环境变量
System.setProperty("jdbc.drivers",drivers);
改为
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNOTFountException e){}
试试
还有就是要正确设置你的环境变量