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());
相關文章
相關標籤/搜索