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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - jdbc實現連接和增刪改查功能

jdbc實現連接和增刪改查功能

2021-07-16 16:05lxh5431 Java教程

這篇文章主要為大家詳細介紹了jdbc實現連接和基本的增刪改查功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

jdbc的定義

jdbc(java data base connectivity,java數據庫連接)是一種用于執行sql語句的java api,可以為多種關系數據庫提供統一訪問,它由一組用java語言編寫的類和接口組成。jdbc提供了一種基準,據此可以構建更高級的工具和接口,使數據庫開發人員能夠編寫數據庫應用程序。

jdbc的基本連接

簡單的說就是加載驅動,建立連接,然后進行查詢和刪除等語句的操作,在java中提供了java.sql的jar包,不過我現在用的是mysql的連接和實例,在這里基本在本地的服務器都是用到下面這個語句。

?
1
2
3
4
5
class.forname("com.mysql.jdbc.driver");
 
  string url="jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8";
  string user="root";
  string password="root";

加載和建立連接,這就是基本的一個語法結構,在連接數據庫當然還有其他的屬性可以設置,比如說最大的連接數了,和如何建立連接池,都可以在配置中用到,這里我就簡單的介紹如何連接,后面跟的是這個連接的字符集,防止出現亂碼。

簡單的增刪改查

簡單的增刪改查是每個開發者都會遇到的,畢竟我們整個系統真正的業務所在也是這幾個簡單的邏輯,但是在關系的連接和耦合性下就會變成復雜百倍的系統,所以要懂得基本的就可以窺見更大系統的構建了,所以我現在要分析的只是基本的功能實現。

首先連接好數據庫之后,那就是創建查詢語句,這里用到的是statment這個關鍵詞,下面是代碼的基本實現。

?
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
152
package dbtest;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.list;
 
 
public class dbtest {
 
 public static void main(string[] args) {
  employee employee1 =new employee();
  employee1.setempno(555);
  employee1.setename("hakly");
  employee1.setsal(5400);
  employee1.sethiredate(new date());
 
  addemployee(employee1);
  list<employee>employees=getemployees();
  for(employee employee:employees){
   system.out.println(employee);
 
  }
 
  employee employee =new employee();
  employee.setempno(999);
  employee.setename("jack");
  employee.setsal(5000);
  employee.sethiredate(new date());
 
  addemployee(employee);
 
 }
 
  public static list<employee> getemployees() {
  resultset rs=null;
  connection conn=null;
  statement stat=null;
  list<employee> employees=new arraylist<employee>();
  try{
   class.forname("com.mysql.jdbc.driver");
 
 
  string url="jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8";
  string user="root";
  string password="root";
  conn= drivermanager.getconnection(url,user,password);
  stat=conn.createstatement();
  string sql="select * from emp";
   rs=stat.executequery(sql);
   employee employee=null;
  while(rs.next()){
   employee=new employee();
   employee.setempno(rs.getint("empno"));
   employee.setename(rs.getstring("ename"));
   employee.setsal(rs.getdouble("sal"));
  employee.sethiredate(rs.getdate("hiredate"));
  employees.add(employee);
  }
 
 
  }catch(exception e ){
   e.printstacktrace();
  }finally{
   try {
    if(conn!=null){
    conn.close();
   }
    }catch (sqlexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   }
  }
  return employees;
 }
  public static void addemployee(employee employee) {
   connection conn = null;
   statement stat = null;
   // 1.注冊驅動程序
   try {
    class.forname("com.mysql.jdbc.driver");
 
    // 2.建立連接
    string url = "jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8";
    string user = "root";
    string password = "root";
    conn = drivermanager.getconnection(url, user, password);
 
    // 3.創建執行語句,發送sql命令
    stat = conn.createstatement();
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
    string sql = "insert into emp(empno,ename,sal,hiredate) values(" + employee.getempno() + ",'"
      + employee.getename() + "'," + employee.getsal() + ",'" + sdf.format(employee.gethiredate()) + "')";
 
    // 4.處理執行結果
    int i = stat.executeupdate(sql);
   } catch (exception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   } finally {
    // 5.關閉資源
    try {
     if (conn != null) {
      conn.close();
     }
    } catch (sqlexception e) {
     // todo auto-generated catch block
     e.printstacktrace();
    }
   }
  }
  public static void updateemployee(employee employee) {
   connection conn = null;
   statement stat = null;
   // 1.注冊驅動程序
   try {
    class.forname("com.mysql.jdbc.driver");
 
    // 2.建立連接
    string url = "jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8";
    string user = "root";
    string password = "root";
    conn = drivermanager.getconnection(url, user, password);
 
    // 3.創建執行語句,發送sql命令
    stat = conn.createstatement();
    simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
    string sql = "update emp set ename='"+employee.getename()+"empno"+employee.getempno()+"sal"+employee.getsal();
    // 4.處理執行結果
    int i = stat.executeupdate(sql);
   } catch (exception e) {
    // todo auto-generated catch block
    e.printstacktrace();
   } finally {
    // 5.關閉資源
    try {
     if (conn != null) {
      conn.close();
     }
    } catch (sqlexception e) {
     // todo auto-generated catch block
     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
37
38
package dbtest;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
 
public class jdbcutil {
 static string url = "jdbc:mysql://localhost:3306/xxx?useunicode=true&characterencoding=utf-8";
 static string user = "root";
 static string password = "root";
 
 static{
  try {
   class.forname("com.mysql.jdbc.driver");
  } catch (classnotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 public static connection getconnection() throws sqlexception{
  string url = "jdbc:mysql://localhost:3306/test2?useunicode=true&characterencoding=utf-8";
  string user = "root";
  string password = "root";
  return drivermanager.getconnection(url, user, password);
 
 }
 public static void free(connection conn){
 
  try {
   if (conn != null) {
    conn.close();
   }
  } catch (sqlexception e) {
   // todo auto-generated catch block
   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
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.niit.jdbc;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.list;
 
public class employeedao1 {
 
 public static void main(string[] args) throws parseexception {
 
  list<employee> employees=getemployees();
  for(employee employee:employees){
   system.out.println(employee);
  }
 
  /*employee employee = new employee();
  employee.setempno(9999);
  employee.setename("tom");
  employee.setsal(6000);
 
  simpledateformat sdf=new simpledateformat("yyyy-mm-dd");
  employee.sethiredate(sdf.parse("2015-07-23"));
 
  //addemployee(employee);
  //updateemployee(employee);
  deleteemployee(9999);*/
 
 
 
 }
 
 public static list<employee> getemployees() {
  connection conn = null;
  statement stat = null;
  resultset rs = null;
  list<employee> employees = new arraylist<employee>();
  // 1.注冊驅動程序
  try {
   //2.獲取連接
   conn=jdbcutil.getconnection();
 
   // 3.創建執行語句,發送sql命令
   stat = conn.createstatement();
   string sql = "select * from emp";
 
   // 4.處理執行結果
   rs = stat.executequery(sql);
   employee employee = null;
   while (rs.next()) {
    employee = new employee();
    employee.setempno(rs.getint("empno"));
    employee.setename(rs.getstring("ename"));
    employee.setsal(rs.getdouble("sal"));
    employee.sethiredate(rs.getdate("hiredate"));
 
    employees.add(employee);
   }
 
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   // 5.關閉資源
   jdbcutil.free(conn);
  }
  return employees;
 }
 
 public static void addemployee(employee employee) {
  connection conn = null;
  statement stat = null;
  // 1.注冊驅動程序
  try {
   //2.獲取連接
   conn=jdbcutil.getconnection();
 
   // 3.創建執行語句,發送sql命令
   stat = conn.createstatement();
   simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
   string sql = "insert into emp(empno,ename,sal,hiredate) values(" + employee.getempno() + ",'"
     + employee.getename() + "'," + employee.getsal() + ",'" + sdf.format(employee.gethiredate()) + "')";
 
   // 4.處理執行結果
   int i = stat.executeupdate(sql);
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   // 5.關閉資源
   jdbcutil.free(conn);
  }
 }
 
 public static void updateemployee(employee employee) {
  connection conn = null;
  statement stat = null;
  // 1.注冊驅動程序
  try {
   //2.獲取連接
   conn=jdbcutil.getconnection();
 
   // 3.創建執行語句,發送sql命令
   stat = conn.createstatement();
 
   simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
   string sql = "update emp set ename='" + employee.getename() + "',sal=" + employee.getsal() + ",hiredate='"
     + sdf.format(employee.gethiredate()) + "' where empno=" + employee.getempno();
 
   // 4.處理執行結果
   int i = stat.executeupdate(sql);
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   // 5.關閉資源
   jdbcutil.free(conn);
  }
 }
 
 public static void deleteemployee(int empno) {
  connection conn = null;
  statement stat = null;
  // 1.注冊驅動程序
  try {
   //2.獲取連接
   conn=jdbcutil.getconnection();
 
   // 3.創建執行語句,發送sql命令
   stat = conn.createstatement();
   simpledateformat sdf = new simpledateformat("yyyy-mm-dd");
   string sql = "delete from emp where empno="+empno;
 
   // 4.處理執行結果
   int i = stat.executeupdate(sql);
  } catch (exception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } finally {
   // 5.關閉資源
   jdbcutil.free(conn);
  }
 }
 
}

這樣看上去就比較清晰和明了,不用分割開來去看代碼,只要調用到那個累才去照這個類的方法,這樣就能夠更加有利于檢查和排錯,維護的間接性的一個軟件的奮斗的目標,雖然只是簡單的優化,卻能夠減輕了我們寫代碼和維護的成本。

總結

通過本次編碼,對面向對象的編程的開發有更加清晰的了解,當然對重構和優化的重要性有更深的了解,在這個軟件里我學會的不僅是代碼的書寫,還學會了代碼的重構不僅需要不斷的提煉,還需要對代碼的細微的觀察。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/lxh5431/article/details/52443417

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本高清不卡视频 | 日韩欧美在线观看 | 男人天堂亚洲 | 国产a级毛片 | 午夜精品福利一区二区三区蜜桃 | 九九综合九九 | www.99精品 | 亚洲三级在线免费观看 | 一区高清| 中文字幕久久久 | 欧美久久精品 | 久久国产综合 | 成人午夜| 欧美成人精品一区二区三区在线看 | 午夜视频免费在线观看 | 99re热精品视频 | 在线91网| 久久精品一区二区国产 | av网站在线免费观看 | 国产在线一区二区三区 | 九九久久免费 | 好吊妞国产欧美日韩免费观看视频 | 日韩成人在线观看 | 日本中文一区二区 | 每日更新在线观看av | 国产免费视频在线 | 中文字幕成人在线 | 黄色日本视频 | 亚洲精品高潮呻吟久久av | 国产精品久久久久久久久久东京 | 日韩中文字幕在线播放 | 久久一区视频 | 成人免费一区二区三区视频软件 | 亚洲免费精品 | 日韩精品一区二区在线 | 久久免费电影 | 日韩久久久久久 | 久久精品影片 | 日韩亚洲一区二区 | 久久大陆| 欧美天堂一区 |