1.从MySQLGUITools安装目录找到mysql-connector-java-5.0.4-bin.jar。(此为mysql驱动包可复制到项目文件下)
2.项目右键->构建路径->配置构建路径->库->添加外部JAR->找到mysql-connector-java-5.0.4-bin.jar->确定
完整项目workspace
连接代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@SuppressWarnings("unused")
public class Conn{
Connection conn=null;
public Connection getConn()
{
try
{
Class.forName("com.mysql.jdbc.Driver").getInterfaces();
}catch(ClassNotFoundException ex){System.out.println(ex.getMessage());}
try{
conn=DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/mysql","root","1234");
}
catch(Exception e)
{
System.out.println("ConnMySqlerror::"+e.getMessage());
System.out.println(e);
}
return conn;
}
}
测试代码如下:
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello,world");
Conn c0 = new Conn();
c0.getConn();
}
}