Home > Database > Mysql Tutorial > body text

在Eclipse连接mysql-----配置jbdc_MySQL

WBOY
Release: 2016-06-01 13:09:54
Original
1210 people have browsed it

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();      }    }  }
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template