一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

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

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

服務器之家 - 編程語言 - Java教程 - Java使用Statement接口執行SQL語句操作實例分析

Java使用Statement接口執行SQL語句操作實例分析

2021-05-13 12:09Young-xy Java教程

這篇文章主要介紹了Java使用Statement接口執行SQL語句操作,結合實例形式詳細分析了Java使用Statement接口針對mysql數據庫進行連接與執行SQL語句增刪改查等相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了java使用statement接口執行sql語句操作的方法。分享給大家供大家參考,具體如下:

statement執行sql語句:

1. 對數據庫的曾刪改操作時,使用stmt.executeupdate(sql)  執行給定 sql 語句,分別為 insert updatedelete.

2. 對數據庫做查詢時,直接使用 stmt.executequery(sql),返回結果可以為一個resultset結果集。

首先做一些準備工作:

①對要進行操作的數據庫表進行封裝,比如說我的數據mydata中的aistu表,用aimember.java進行封裝,以便后面操作。具體如下:

?
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
package com.mysqltest.jdbc.model;
/**
 * 定義一個model
 * 成員模型
 * @author ai_stu
 *
 */
public class aimember {
  private string name;
  private int id;
  private int age;
  private string email;
  private string tel;
  private double salary;
  private string riqi;
  /**
   * alt+shift+s 添加構造函數generating constructor using fields.
   * @param name
   * @param id
   * @param age
   * @param email
   * @param tel
   * @param salary
   * @param riqi
   */
  public aimember(string name, int id, int age, string email, string tel, double salary, string riqi) {
    super();
    this.name = name;
    this.id = id;
    this.age = age;
    this.email = email;
    this.tel = tel;
    this.salary = salary;
    this.riqi = riqi;
  }
  //重構
  public aimember(int id) {
    super();
    this.id = id;
  }
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public int getid() {
    return id;
  }
  public void setid(int id) {
    this.id = id;
  }
  public int getage() {
    return age;
  }
  public void setage(int age) {
    this.age = age;
  }
  public string getemail() {
    return email;
  }
  public void setemail(string email) {
    this.email = email;
  }
  public string gettel() {
    return tel;
  }
  public void settel(string tel) {
    this.tel = tel;
  }
  public double getsalary() {
    return salary;
  }
  public void setsalary(double salary) {
    this.salary = salary;
  }
  public string getriqi() {
    return riqi;
  }
  public void setriqi(string riqi) {
    this.riqi = riqi;
  }
}

②對連接mysql數據庫,和關閉連接方法進行封裝,這里用dbutil.java進行封裝,具體如下:

?
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
package com.mysqltest.jdbc.modelcomp;
public class compmember {
  private int id;
  private string name;
  private int age;
  private double salary;
  /**
   * 構造函數1
   * @param name
   * @param age
   * @param salary
   */
  public compmember(string name, int age, double salary) {
    super();
    this.name = name;
    this.age = age;
    this.salary = salary;
  }
  /**
   * 重載構造函數
   * @param id
   * @param name
   * @param age
   * @param salary
   */
  public compmember(int id, string name, int age, double salary) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
    this.salary = salary;
  }
  /**
   * get,set方法
   */
  public int getid() {
    return id;
  }
  public void setid(int id) {
    this.id = id;
  }
  public string getname() {
    return name;
  }
  public void setname(string name) {
    this.name = name;
  }
  public int getage() {
    return age;
  }
  public void setage(int age) {
    this.age = age;
  }
  public double getsalary() {
    return salary;
  }
  public void setsalary(double salary) {
    this.salary = salary;
  }
  @override
  /**
   * 改寫tostring,使得顯示更好
   */
  public string tostring() {
    return "["+this.id+"]"+this.name+","+this.age+","+this.salary;
  }
}

準備工作做好了,下面開始使用statement接口執行sql語句來實現增刪改:

①增:

?
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
package com.mysqltest.jdbc.two2;
import java.sql.connection;
import java.sql.statement;
import com.mysqltest.jdbc.model.aimember;
import com.mysqltest.jdbc.util.dbutil;
public class demo3 {
  /**
   * 添加成員到表中1
   * @param name
   * @param id
   * @param age
   * @param email
   * @param tel
   * @param salary
   * @param riqi
   * @return
   * @throws exception
   */
  @suppresswarnings("unused")
  private static int addmember(string name,int id,int age,string email,string tel,double salary,string riqi) throws exception{
    dbutil dbutil = new dbutil();//之前封裝好的
    connection con = dbutil.getcon(); //獲取數據庫連接
    string sql = "insert into aistu values('"+name+"',"+id+",'"+age+"','"+email+"','"+tel+"','"+salary+"','"+riqi+"')";
    statement stmt = con.createstatement();//獲取statement
    int result = stmt.executeupdate(sql);
    dbutil.close(stmt, con);
    return result;
  }
  /**
   * 添加成員到表中2方法
   * @param mem
   * @return
   * @throws exception
   */
  private static int addmember2(aimember mem) throws exception{ //aimember也是之前封裝好的
//    mem.getname();
    dbutil dbutil = new dbutil();//之前封裝好的
    connection con = dbutil.getcon(); //獲取數據庫連接
    string sql = "insert into aistu values('"+mem.getname()+"',"+mem.getid()+",'"+mem.getage()+"','"+mem.getemail()+"','"+mem.gettel()+"','"+mem.getsalary()+"','"+mem.getriqi()+"')";
    statement stmt = con.createstatement();//獲取statement
    int result = stmt.executeupdate(sql);
    dbutil.close(stmt, con);
    return result;
  }
//  private static int addmenber2()
  public static void main(string[] args) throws exception {
    /*int result = addmember("劉翔", 4, 28, "[email protected]", "13411957776", 8000.00, "2015-09-10");
    if(result==1){
      system.out.println("添加成功");
    }else{
      system.out.println("添加失敗");
    }*/ //多行注釋,ctrl+shift+/
    aimember mem = new aimember("李娜", 6, 25, "[email protected]", "13411957775", 8000.00, "2015-09-03");
    int result = addmember2(mem);
    if(result==1){
      system.out.println("添加成功");
    }else{
      system.out.println("添加失敗");
    }
  }
}

②改:

?
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
package com.mysqltest.jdbc.two3;
import java.sql.connection;
import java.sql.statement;
import com.mysqltest.jdbc.model.aimember;
import com.mysqltest.jdbc.util.dbutil;
public class demo4 {
  private static dbutil dbutil = new dbutil();
//  @suppresswarnings("unused")
  /**
   * 修改成員
   * @param mem
   * @return
   * @throws exception
   */
  private static int updatemember(aimember mem) throws exception {
    connection con = dbutil.getcon(); // 獲取數據庫連接
    string sql = "update aistu set name='" + mem.getname() + "',id=" + mem.getid() + ",age='" + mem.getage()
        + "',email='" + mem.getemail() + "',tel='" + mem.gettel() + "',salary='" + mem.getsalary() + "',riqi='"
        + mem.getriqi() + "' where id=" + mem.getid();
    //格式化,ctrl+a全選,然后ctrl+shift+f格式化
    statement stmt = con.createstatement();// 獲取statement
    int result = stmt.executeupdate(sql);
    dbutil.close(stmt, con);
    return result;
//    return 0;
  }
  public static void main(string[] args) throws exception {
    aimember mem = new aimember("勞爾", 6, 24, "[email protected]", "13411957770", 18000.00, "2014-09-03");
    int result = updatemember(mem);
    if (result==1) {
      system.out.println("更新成功");
    } else {
      system.out.println("更新失敗");
    }
  }
}

③刪:

?
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
package com.mysqltest.jdbc.two4;
import java.sql.connection;
import java.sql.statement;
import com.mysqltest.jdbc.model.aimember;
import com.mysqltest.jdbc.util.dbutil;
public class demo5 {
  private static dbutil dbutil = new dbutil();
  public static int deletmember(aimember mem) throws exception{
    connection con = dbutil.getcon(); // 獲取數據庫連接
    string sql = "delete from aistu where id="+mem.getid();
    statement stmt = con.createstatement();// 獲取statement
    int result = stmt.executeupdate(sql);
    dbutil.close(stmt, con);
    return result;
  }
  public static void main(string[] args) throws exception {
    aimember mem = new aimember(5);
    int result = deletmember(mem);
    if (result==1) {
      system.out.println("成功刪除成員");
    } else {
      system.out.println("刪除成員失敗");
    }
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://blog.csdn.net/u010986080/article/details/51812936

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2019中文字幕 | 视频在线免费看 | 午夜免费啪视频观看视频 | 国产综合亚洲欧美日韩一区二区 | www.国产在线观看 | 国产一区二区在线观看美女 | 激情综合色啪啪小说 | 91精品国产91热久久久久福利 | 天美影视传媒mv直接看 | h版欧美大片免费观看 | 97视频免费人人观看人人 | 黑人巨大vs北条麻妃在线 | 亚洲一二三区视频 | 日本成年片高清在线观看 | 免费观看网站 | 32pao强力打造免费高速高清 | 亚洲无限 | 国内精品一区视频在线播放 | 日你逼 | 国产精品国产三级国产专区不 | 成人国产精品一级毛片视频 | 精品国偷自产在线 | 性趣味商品推荐 | 日韩精品免费一级视频 | 国内外成人在线视频 | 成人高清网站 | 日韩在线二区 | 荡女淫春2古装 | 调教女高中生第3部分 | 狠狠澡 | 亚洲天堂激情 | 国产亚洲一欧美一区二区三区 | 日本xxxx18vr69 | 欧美亚洲国产精品久久久 | 爆操美女在线观看 | 日本一区二区三区久久 | 99久久精品国产免费 | 激情综合| 美琪美腿白丝交小说 | 国产亚洲综合成人91精品 | 亚洲欧洲日产国码无码av |