首先安装 mysql 的java驱动
http://dev.mysql.com/downloads/connector/j/
然后把 mysql-connector-java-5.1.17-bin.jar 放到jdk/jre/lib/ext 下面
下面是java连接mysql的代码
String url ="jdbc:mysql://"+ host +":"+ port +"/"+ database +"?useUnicode=true&characterEncoding="+ENCODING;
try
{
Connection conn = DriverManager.getConnection(url, username, password);
if (!conn.isClosed())
{
Debug.println("数据库连接成功!");
query(conn, client, sql);
}
else
{
client.stopThread();
Debug.println("数据库已关闭!");
}
}
catch (SQLException e)
{
client.stopThread();
Debug.println("数据库连接失败!");
}