我在Myssql command line上可以访问,但是在用如下代码就出现了以上的错误:
import java.sql.*;
public class SampleIntro
{
public static void main(String[] args)
{
try
{
Connection conn;
Statement stmt;
ResultSet res;
//加载Connector/J驱动
Class clas=Class.forName("org.gjt.mm.mysql.Driver");
System.out.println(clas.toString()+"suceesfully");
// Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "zhoudeliang");
//执行SQL语句
System.out.println("成功连接: jdbc:mysql://localhost:3306/test");
stmt = conn.createStatement();
res = stmt.executeQuery("select * from pet");
//处理结果集
while (res.next())
{
String name = res.getString("name");
System.out.println(name);
}
res.close(); }
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
import java.sql.*;
public class SampleIntro
{
public static void main(String[] args)
{
try
{
Connection conn;
Statement stmt;
ResultSet res;
//加载Connector/J驱动
Class clas=Class.forName("org.gjt.mm.mysql.Driver");
System.out.println(clas.toString()+"suceesfully");
// Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "zhoudeliang");
//执行SQL语句
System.out.println("成功连接: jdbc:mysql://localhost:3306/test");
stmt = conn.createStatement();
res = stmt.executeQuery("select * from pet");
//处理结果集
while (res.next())
{
String name = res.getString("name");
System.out.println(name);
}
res.close(); }
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
String drivername = "com.mysql.jdbc.Driver";
....
String url = "jdbc:mysql://" + serverip + ":" + port + "/" + dbname
+ "?useUnicode=true&characterEncoding=utf8";
.......
DBConnectionPool pool = new DBConnectionPool(dbname, url, user, password, 10, 1);
....
注意以上几点,试试看。
Welcome to the MySQL monitor. Commands end with; or \g.
Your MySQL connection id is 70 to server version: 5.0.18-ntType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> use test
Database changed
mysql> desc pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.01 sec)mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | 0000-00-00 |
| Claws | Gwen | cat | m | 1994-03-17 | 0000-00-00 |
| Buffy | Harold | dog | f | 1989-05-13 | 0000-00-00 |
| Fang | Benny | dog | m | 1990-08-27 | 0000-00-00 |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | 0000-00-00 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | 0000-00-00 |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)mysql>
以上是我在commmand line 下的测试,没有错误,但用上面的那个程序连接就不行了;Class.forName("org.gjt.mm.mysql.Driver");是成功执行,就是在conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "zhoudeliang");
发生了异常,说明:test为数据库,pet为其中的一个表,创建了用户名root和password is "zhoudelaing"