国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - JDBC增刪改查和查唯一的完整代碼解析

JDBC增刪改查和查唯一的完整代碼解析

2020-07-19 11:46long_street_to_walk Java教程

這篇文章主要介紹了JDBC增刪改查和查唯一的完整代碼解析,代碼分為第四部分,每部分代碼都不錯,對jdbc增刪改查操作感興趣的朋友一起學(xué)習(xí)吧

第一部分代碼(實體類)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.wf.entity;
public class Hehe{
private int hehe_id;
private String hehe_name;
private String hehe_gender;
public int getHehe_id(){
return hehe_id;
}
public void setHehe_id(int heheId){
hehe_id=heheId;
}
public String getHehe_name() {
return hehe_name;
}
public void setHehe_name(String heheName) {
hehe_name = heheName;
}
public String getHehe_gender() {
return hehe_gender;
}
public void setHehe_gender(String heheGender) {
hehe_gender = heheGender;
}
}

第二部分 BaseDao(增刪改查和查唯一)

?
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
package com.wf.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao{
protected static final String DRIVER="com.mysql.jdbc.Driver";
protected static final String URL="mysql://localhost:3306/mysql";
protected static final String USER="root";
protected static final String PASSWORD="******";
protected Connection connection=null;
protected PreparedStatement preparedStatement=null;
protected ResultSet resultSet =null;
protected void getconnection() throws ClassNotFoundException, SQLException{
  Class.forName(DRIVER);
  this.connection =DriverManager.getconnection (URL,USER,PASSWORD);
}
/**
* 通用的增刪改方法
* @param sql SQL語句
* @param params 參數(shù)數(shù)組
* @return 受影響的行數(shù)
*/
protected int executeUpdate(String sql ,String[]params){
int result=-1;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
result= this.preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.close();
}
return result;
}
/**
* 查詢結(jié)果集的方法
* @param sql
* @param params
*/
protected void executeQuery(String sql,String[]params){
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
this.resultSet=this.preparedStatement.executeQuery();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
?
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
/**
* 查詢唯一的結(jié)果
* @return Object
*/
protected Object executeQueryUnique(String sql,String[]params){
Object object=null;
try {
this.getconnection();
this.preparedStatement=this.connection.prepareStatement(sql);
if(null!=params){
for (int i = 0; i < params.length; i++) {
this.preparedStatement.setString(i+1, params[i]);
}
}
this.resultSet=this.preparedStatement.executeQuery();
if(this.resultSet.next())
object=this.resultSet.getObject(1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return object;
}
protected void close(){
try {
if(null!=this.resultSet)
this.resultSet.close();
if(null!=this.preparedStatement)
this.preparedStatement.close();
if(null!=this.connection)
this.connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

第三部分 HeheDao

?
1
2
3
4
5
6
7
8
9
10
11
package com.wf.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wf.entity.Hehe;
public class HeheDao extends BaseDao{
public boolean insert(Hehe hehe){
String sql="insert into hehe(hehe_name,hehe_gender) values(?,?)" ;
String []params=new String[]{hehe.getHehe_name(),hehe.getHehe_gender()};
return this.executeUpdate(sql, params)>0? true:false;
}

第四部分 測試Test_BaseDao_Insert

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.wf.test;
import com.wf.dao.HeheDao;
import com.wf.entity.Hehe;
public class Test_BaseDao_Insert {
public static void main(String[] args) {
Hehe hehe=new Hehe();
hehe.setHehe_name("個");
hehe.setHehe_gender("b");
HeheDao _hd=new HeheDao();
if(_hd.insert(hehe))
System.out.println("成功!");
else
System.out.println("失敗!");
}
}

原文鏈接:http://www.cnblogs.com/wffj150926/archive/2016/12/25/6219908.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久久久久久影院 | 中文字幕亚洲一区二区va在线 | 亚洲国产91 | 久久久久久久久国产精品 | 精品久久av | 亚洲精品在线免费看 | 搞黄免费视频 | 男人天堂网站 | 亚洲片国产一区一级在线观看 | 欧美日韩在线一区 | 黄色网页免费看 | 日日嗨av一区二区三区四区 | 一区二区三区四区免费看 | 午夜私人影院 | 黄网av | 成人综合在线观看 | 国产精品久久久久国产精品 | 婷婷亚洲五月 | 亚洲国产一级 | 一区二区三区国产视频 | 亚洲h视频| 欧美一区二区在线免费观看 | 在线色av| 国产精品无码久久久久 | 久久久久九九九九九 | 国产一区二区精品丝袜 | 欧美第一视频 | 大片免费播放在线观看视频 | 国产a在亚洲线播放 | 欧美啪啪 | 欧美电影在线观看 | 在线小视频国产 | 伊人亚洲 | 日本一区二区三区在线视频 | 日本末发育嫩小xxxx | 国产视频色 | 欧美精品亚洲精品 | 黄色小视频在线免费观看 | 久久精品影片 | 精品一二三区 | 久久777 |