집 >데이터 베이스 >MySQL 튜토리얼 >在Eclipse连接mysql-----配置jbdc_MySQL
1、 安装mysql这里就不详述了,详细可以参考我的博客;
http://blog.csdn.net/u014487509/article/details/26340741
2、下载jdbc:http://www.mysql.com/products/connector/;
3、将mysql-connector-java-5.1.7-bin.jar包拷贝到jdk的安装路径中的lib包中;
4、new一个Dynamic web工程;
5、项目名称右键单击选择Build Path->Configure Build Path,在弹出的窗口选择Libraries->Add External JAR,然后选择mysql-connector-java-5.1.7-bin.jar,点击ok;
6、接下来就可以测试数据库是否可以连接成功了。
import java.sql.*; public class Sql { public static void main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","123456"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 System.out.println("Success connect Mysql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from test"); //user 为你表的名称 while (rs.next()) { System.out.println(rs.getString("name")); } } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace(); } } }