服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - 用Eclipse连接MySQL数据库的步骤

用Eclipse连接MySQL数据库的步骤

2020-05-17 16:17MYSQL教程网 Mysql

这篇文章主要介绍了如何用Eclipse连接MySQL数据库,需要的朋友可以参考下

我的环境:MySQL:mysql-essential-5.1.51-win32

安装之前的准备工作:

jdbc驱动

Eclipse:任意版本,免费的,可以百度的到。

1.MySQL安装

下面来创建一个数据:

?
1
2
3
4
mysql>CREATE DATABASE test;  //创建一个数据库
mysql>use test; //指定test为当前要操作的数据库
mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20));  //创建一个表user,设置两个字段。
mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一条数据到表中

2.打开Eclipse,创建一个项目(my),

操作:右键点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。

用Eclipse连接MySQL数据库的步骤

我的项目列表:

用Eclipse连接MySQL数据库的步骤

3.驱动已经导入,下面我们来写一个程序验证一下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.sql.*;
public class MysqlJdbc {
 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","198876");
      //连接URL为  jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码
 
   System.out.println("Success connect Mysql server!");
   Statement stmt = connect.createStatement();
   ResultSet rs = stmt.executeQuery("select * from user");
                               //user 为你表的名称
   while (rs.next()) {
    System.out.println(rs.getString("name"));
   }
  }
  catch (Exception e) {
   System.out.print("get data error!");
   e.printStackTrace();
  }
 }
}

点击运行程序:

?
1
2
Success loading Mysql Driver!
Success connect Mysql server!

出现上面结果,说明你连接数据库成功。

4.可以查看到MySQL里面的内容,那我们是不是想往MySQL中插入数据呢。

下面的例子,往MySQL的user表中插入100条数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.sql.*;
 
public class Myjproject {
 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","198876");
   
    int num=100;
    PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");
    for(int i=0;i<num;i++)    //定义个100次的循环,往表里插入一百条信息。
   {
      Statement.setString(1,"chongshi"+i);
      Statement.setString(2,"bo"+i);
      Statement.executeUpdate();
  }
 
 // } catch (ClassNotFoundException e) {
  // TODO Auto-generated catch block
  // System.out.println("An error has occurred:"+e.toString());
 // e.printStackTrace();
  }catch(SQLException e)
  {
  }
 }
}

5.下面我们打开MySQL数据库进行查看

?
1
2
3
4
5
mysql> show tatabases; //查看所数据库
mysql> use test;  //使test为当前要操作的数据库
mysql> show tables; //查看当前数据库的所有表
view sourceprint?
mysql> select *from user; //查看当前表(user)的所有信息

注意:如果不能正常连接你的数据库,请检查你代码中,驱动、用户名、密码、表等信息是否对应无误,不要把别人的代码直接复制过来,看也不看就用。

以上就是用Eclipse连接MySQL数据库的全部过程,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐