当前位置: 技术问答>java相关
jdbc连接mysql的问题?
来源: 互联网 发布时间:2014-12-27
本文导语: 我用mysql的driver连接mysql,写了如下的一段代码: import java.sql.*; public class LoadDriver{ public static void main(String[] Args){ try { ...
我用mysql的driver连接mysql,写了如下的一段代码:
import java.sql.*;
public class LoadDriver{
public static void main(String[] Args){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("yes load it!");
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try{
Connection sqlConn;
sqlConn=DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=banyi;useUnicode=true");
}
catch(Exception e){
System.err.println("can't connect!");
e.printStackTrace();
}
}
}
运行后:
yes load it!
java.lang.ArrayIndexOutOfBoundsException
at org.gjt.mm.mysql.Buffer.writeString(Buffer.java:318)
at org.gjt.mm.mysql.MysqlIO.init(MysqlIO.java:252)
at org.gjt.mm.mysql.Connection.(Connection.java:230)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at LoadDriver.main(LoadDriver.java:29)
can't connect!
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.ArrayIndexOutOfBoundsException)
at org.gjt.mm.mysql.Connection.(Connection.java:239)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at LoadDriver.main(LoadDriver.java:29)
这是为什么?
import java.sql.*;
public class LoadDriver{
public static void main(String[] Args){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.out.println("yes load it!");
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try{
Connection sqlConn;
sqlConn=DriverManager.getConnection("jdbc:mysql://localhost/bbs?user=root&password=banyi;useUnicode=true");
}
catch(Exception e){
System.err.println("can't connect!");
e.printStackTrace();
}
}
}
运行后:
yes load it!
java.lang.ArrayIndexOutOfBoundsException
at org.gjt.mm.mysql.Buffer.writeString(Buffer.java:318)
at org.gjt.mm.mysql.MysqlIO.init(MysqlIO.java:252)
at org.gjt.mm.mysql.Connection.(Connection.java:230)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at LoadDriver.main(LoadDriver.java:29)
can't connect!
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.ArrayIndexOutOfBoundsException)
at org.gjt.mm.mysql.Connection.(Connection.java:239)
at org.gjt.mm.mysql.Driver.connect(Driver.java:126)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at LoadDriver.main(LoadDriver.java:29)
这是为什么?
|
jdbc:mysql://[hostname][:port]/dbname[?param1=value1][=value2]...
...
try {
Connection C = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=monty=greatsqldb");
// Do something with the Connection
....
}
catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
...
try {
Connection C = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=monty=greatsqldb");
// Do something with the Connection
....
}
catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}