java jdbc链接mysql数据库实现增删改查操做

这篇文章主要为你们详细介绍了java jdbc链接mysql数据库实现增删改查操做,须要的朋友能够参考下

jdbc相信你们都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打交道很是基础的一个知识,也是比较接近底层的,在实际的工做中你们用得更多的其实仍是比较成熟的框架,例如Hibernate、Mybatis。 

可是做为这些成熟框架的底层的jdbc却也是咱们应该去掌握的,只有了解了jdbc的增删改查,这样在之后若是有兴趣去研究Hibernate或者Mybatis的源代码的时候才能更好的去理解这些成熟的框架是如何去实现增删改查的。 
java

回归正题,先来看看咱们的开发环境: 
mysql

Java语言、Eclipse开发工具、Mysql数据库、Navicat数据库可视化工具。 
sql

开发环境的安装搭建及使用请本身查阅资料(很简单的),这里不详细阐述。 
数据库

第一步,建立数据库,利用Navicat数据库可视化工具随便创建一个数据库,在库中创建一张表,表里给几个字段(记得给个id字段,惟一主键,自增序列),再随便给上两条数据便好,用来测试功能,如图: mvc

第二步,打通数据库(这个例子但愿你们本身动手敲敲,耽误不了多少时间,熟悉一下jdbc如何和数据库打交道,故以图示之),如图:
app

第三步,改造DBUtil类,方便在dao层得到数据库的链接,代码以下:
框架

?
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
36
37
38
39
package com.czgo.db;
 
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DBUtil
{
   private static final String URL = "jdbc:mysql://127.0.0.1:3306/imooc" ;
   private static final String UNAME = "root" ;
   private static final String PWD = "root" ;
 
   private static Connection conn = null ;
 
   static
   {
     try
     {
       // 1.加载驱动程序
       Class.forName( "com.mysql.jdbc.Driver" );
       // 2.得到数据库的链接
       conn = DriverManager.getConnection(URL, UNAME, PWD);
     }
     catch (ClassNotFoundException e)
     {
       e.printStackTrace();
     }
     catch (SQLException e)
     {
       e.printStackTrace();
     }
   }
 
   public static Connection getConnection()
   {
     return conn;
   }
}

第四步,建立实体类(如上图,你们观察包的分配,咱们将采用MVC思想设计本实例,有关于mvc的设计思想,请你们自行学习,这里很少说)代码以下:
工具

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.czgo.model;
 
import java.io.Serializable;
 
/**
  * 实体类:女神类
  *
  * @author AlanLee
  *
  */
public class Goddess implements Serializable {
   private static final long serialVersionUID = 1L;
 
   /**
    * 惟一主键
    */
   private Integer id;
   /**
    * 姓名
    */
   private String name;
   /**
    * 手机号码
    */
   private String mobie;
   /**
    * 电子邮件
    */
   private String email;
   /**
    * 家庭住址
    */
   private String address;
 
   public Integer getId()
   {
     return id;
   }
 
   public void setId(Integer id)
   {
     this .id = id;
   }
 
   public String getName()
   {
     return name;
   }
 
   public void setName(String name)
   {
     this .name = name;
   }
 
   public String getMobie()
   {
     return mobie;
   }
 
   public void setMobie(String mobie)
   {
     this .mobie = mobie;
   }
 
   public String getEmail()
   {
     return email;
   }
 
   public void setEmail(String email)
   {
     this .email = email;
   }
 
   public String getAddress()
   {
     return address;
   }
 
   public void setAddress(String address)
   {
     this .address = address;
   }
}

第五步,dao层的实现(这里因为是小例子没有写dao接口,实际工做中大型项目应该是要写dao接口的,便于程序的维护和扩展),代码以下:
学习

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.czgo.dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.czgo.db.DBUtil;
import com.czgo.model.Goddess;
 
/**
  * 数据层处理类
  *
  * @author AlanLee
  *
  */
public class GoddessDao
{
   /**
    * 查询所有女神
    *
    * @return
    * @throws SQLException
    */
   public List<Goddess> query() throws SQLException
   {
     List<Goddess> goddessList = new ArrayList<Goddess>();
 
     // 得到数据库链接
     Connection conn = DBUtil.getConnection();
 
     StringBuilder sb = new StringBuilder();
     sb.append( "select id,name,mobie,email,address from goddess" );
 
     // 经过数据库的链接操做数据库,实现增删改查
     PreparedStatement ptmt = conn.prepareStatement(sb.toString());
 
     ResultSet rs = ptmt.executeQuery();
 
     Goddess goddess = null ;
 
     while (rs.next())
     {
       goddess = new Goddess();
       goddess.setId(rs.getInt( "id" ));
       goddess.setName(rs.getString( "name" ));
       goddess.setMobie(rs.getString( "mobie" ));
       goddess.setEmail(rs.getString( "email" ));
       goddess.setAddress(rs.getString( "address" ));
 
       goddessList.add(goddess);
     }
     return goddessList;
   }
 
   /**
    * 查询单个女神
    *
    * @return
    * @throws SQLException
    */
   public Goddess queryById(Integer id) throws SQLException
   {
     Goddess g = null ;
 
     Connection conn = DBUtil.getConnection();
 
     String sql = "" + " select * from imooc_goddess " + " where id=? " ;
 
     PreparedStatement ptmt = conn.prepareStatement(sql);
 
     ptmt.setInt( 1 , id);
 
     ResultSet rs = ptmt.executeQuery();
 
     while (rs.next())
     {
       g = new Goddess();
       g.setId(rs.getInt( "id" ));
       g.setName(rs.getString( "name" ));
       g.setMobie(rs.getString( "mobie" ));
       g.setEmail(rs.getString( "email" ));
       g.setAddress(rs.getString( "address" ));
     }
 
     return g;
   }
 
   /**
    * 添加女神
    *
    * @throws SQLException
    */
   public void addGoddess(Goddess goddess) throws SQLException
   {
     // 得到数据库链接
     Connection conn = DBUtil.getConnection();
 
     String sql = "insert into goddess(name,mobie,email,address) values(?,?,?,?)" ;
 
     PreparedStatement ptmt = conn.prepareStatement(sql);
 
     ptmt.setString( 1 , goddess.getName());
     ptmt.setString( 2 , goddess.getMobie());
     ptmt.setString( 3 , goddess.getEmail());
     ptmt.setString( 4 , goddess.getAddress());
 
     ptmt.execute();
   }
 
   /**
    * 修改女神资料
    *
    * @throws SQLException
    */
   public void updateGoddess(Goddess goddess) throws SQLException
   {
     Connection conn = DBUtil.getConnection();
 
     String sql = "update goddess set name=?,mobie=?,email=?,address=? where id=?" ;
 
     PreparedStatement ptmt = conn.prepareStatement(sql);
 
     ptmt.setString( 1 , goddess.getName());
     ptmt.setString( 2 , goddess.getMobie());
     ptmt.setString( 3 , goddess.getEmail());
     ptmt.setString( 4 , goddess.getAddress());
 
     ptmt.execute();
   }
 
   /**
    * 删除女神
    *
    * @throws SQLException
    */
   public void deleteGoddess(Integer id) throws SQLException
   {
     Connection conn = DBUtil.getConnection();
 
     String sql = "delete from goddess where id=?" ;
 
     PreparedStatement ptmt = conn.prepareStatement(sql);
 
     ptmt.setInt( 1 , id);
 
     ptmt.execute();
   }
}

第六步,控制层的实现(控制层在此处用来模仿控制层和界面,直接在这里构建数据,若是是界面的数据则经过请求传递接收参数便可,控制层的代码你们能够根据实际状况去更改完善,这里只是给你们抛砖引玉,作个简单的测试,时间比较紧,但愿你们理解),代码以下:
开发工具

?
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.czgo.action;
 
import java.sql.SQLException;
import java.util.List;
 
import com.czgo.dao.GoddessDao;
import com.czgo.model.Goddess;
 
/**
  * 控制层,直接在这里构建数据,界面的数据则经过请求传递接收便可,亦是同理
  *
  * @author AlanLee
  *
  */
public class GoddessAction
{
   /**
    * 新增女神
    *
    * @param goddess
    * @throws Exception
    */
   public void add(Goddess goddess) throws Exception
   {
     GoddessDao dao = new GoddessDao();
     goddess.setName( "苍井空" );
     goddess.setMobie( "52220000" );
     goddess.setEmail( "52220000@qq.com" );
     goddess.setAddress( "北京红灯区" );
     dao.addGoddess(goddess);
   }
 
   /**
    * 查询单个女神
    *
    * @param id
    * @return
    * @throws SQLException
    */
   public Goddess get(Integer id) throws SQLException
   {
     GoddessDao dao = new GoddessDao();
     return dao.queryById(id);
   }
 
   /**
    * 修改女神
    *
    * @param goddess
    * @throws Exception
    */
   public void edit(Goddess goddess) throws Exception
   {
     GoddessDao dao = new GoddessDao();
     dao.updateGoddess(goddess);
   }
 
   /**
    * 删除女神
    *
    * @param id
    * @throws SQLException
    */
   public void del(Integer id) throws SQLException
   {
     GoddessDao dao = new GoddessDao();
     dao.deleteGoddess(id);
   }
 
   /**
    * 查询所有女神
    *
    * @return
    * @throws Exception
    */
   public List<Goddess> query() throws Exception
   {
     GoddessDao dao = new GoddessDao();
     return dao.query();
   }
 
   /**
    * 测试是否成功
    *
    * @param args
    * @throws SQLException
    */
   public static void main(String[] args) throws SQLException
   {
     GoddessDao goddessDao = new GoddessDao();
 
     List<Goddess> goddessList = goddessDao.query();
 
     for (Goddess goddess : goddessList)
     {
       System.out.println(goddess.getName() + "," + goddess.getMobie() + "," + goddess.getEmail());
相关文章
相关标签/搜索