首先下载java连接mysql数据库的驱动jar包,然后把.jar包所在的路径复制到classpath中,然后再把.jar包导入到eclipse中,接下来具体操作是:
public Connection getConn(){
Connection conn =null;
try
{
String url="jdbc:mysql://localhost/login";
String user="root";
String pwd="";
//加载驱动,
Class.forName("com.mysql.jdbc.Driver");
//建立到MySQL的连接
conn = DriverManager.getConnection(url,user, pwd);
//执行SQL语句
Statement stmt = conn.createStatement();//创建语句对象,用以执行sql语言
ResultSet rs = stmt.executeQuery("select * from message");//返回的结果集
rs.close();
conn.close();
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
return conn;
}
java连接MySql数据库的jar包下载地址http://download.csdn.net/detail/u010887227/6340175;