使用控制台進行數據庫增刪改查操作,首先創建一個Java Bean類,實現基礎數據的構造,Get,Set方法的實現,減少代碼重復性。
基本屬性為 學生學號 Id, 學生姓名 Name,學生性別 Sex, 學生年齡 Age
1 public class Student { 2 3 //學生類需要的屬性,都是私有的 4 private String Id; 5 private String Name; 6 private String Sex; 7 private String Age; 8 9 //Get Set 方法的實現 10 public String getId() { 11 return Id; 12 } 13 public void setId(String id) { 14 Id = id; 15 } 16 public String getName() { 17 return Name; 18 } 19 public void setName(String name) { 20 Name = name; 21 } 22 public String getSex() { 23 return Sex; 24 } 25 public void setSex(String sex) { 26 Sex = sex; 27 } 28 public String getAge() { 29 return Age; 30 } 31 public void setAge(String age) { 32 Age = age; 33 } 34 //構造方法 35 public Student(String id, String name, String sex, String age) { 36 super(); 37 Id = id; 38 Name = name; 39 Sex = sex; 40 Age = age; 41 } 42 public Student(String string, String string2, String string3) { 43 // TODO Auto-generated constructor stub 44 this.Id=null; 45 this.Name=string; 46 this.Age=string3; 47 this.Sex=string2; 48 } 49 50 51 }
其次是Java Dao類,實現對數據庫的連接,和通過sql語句實現增刪改查。
getRowCount 獲取行的數量
getColumnCount獲取列的數量
//接口
Connection conn= null;
//加載 JDBC 橋的驅動程序
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/user? serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false";
(user 處 為數據庫名稱)
String username = "root";
String password = "101032";
//使用sql 包中的 Connection 接口,通過DriverManger 類的靜態方法getConnection()創建連接對象
conn=DriverManager.getConnection(url,username,password);
String sql="insert into students(Name,Sex,Age) values(?,?,?)";
PreparedStatement pstmt; (PreparedStatement類型執行 為動態sql 語句
如 插入 insert 更新 update 刪除 delete)
execute(String sql) 執行sql語句 返回 ResultSet 對象
executeUpdate(String sql) 執行動態的sql語句
Statement 執行靜態sql 語句
方法 execute(String sql)執行靜態 查找 select 語句,可能返回多個結果集
executeQuery(String sql) 返回單個 ResultSet 對象
1 /** 2 * 數據庫的連接 3 * 實現數據庫的增刪改查 4 */ 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 11 import com.Bean.Student; 12 13 public class DAO { 14 15 private static Connection getConn() { 16 Connection conn =null; 17 18 try { 19 //加載 JDBC 橋的驅動程序 20 Class.forName("com.mysql.cj.jdbc.Driver"); 21 } catch (ClassNotFoundException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } // 加載對應驅動 25 26 //通過數據庫的 url 訪問數據庫 27 String url="jdbc:mysql://localhost:3306/user?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false"; 28 //mysql 數據庫的用戶名 29 String username = "root"; 30 //mysql 數據庫密碼 31 String password = "101032"; 32 33 try { 34 //使用sql 包中的 Connection 接口,通過DriverManger 類的靜態方法getConnection()創建連接對象 35 conn=DriverManager.getConnection(url,username,password); 36 } catch (SQLException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } 40 41 return conn; 42 } 43 44 //數據庫查找操作 45 public static int insert(Student student) { 46 // Connection 接口代表與特定的數據庫連接 並在連接上下文中執行sql 語句 47 Connection conn = getConn(); 48 int i=0; 49 // sql 語句的查找 Name,Sex,Age 插入的數據屬性 50 String sql="insert into students(Name,Sex,Age) values(?,?,?)"; 51 PreparedStatement pstmt; 52 53 try { 54 //執行 動態的sql 語句 55 pstmt = conn.prepareStatement(sql); 56 //將獲取到的 Name,Sex,Age 的數據插入到指定的位置 57 pstmt.setString(1, student.getName()); 58 pstmt.setString(2, student.getSex()); 59 pstmt.setString(3, student.getAge()); 60 i=pstmt.executeUpdate(); 61 pstmt.close(); 62 conn.close(); 63 } catch (SQLException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 68 return i; 69 } 70 71 //數據庫的修改 72 public static int updata(Student student) { 73 // Connection 接口代表與特定的數據庫連接 並在連接上下文中執行sql 語句 74 Connection conn= getConn(); 75 int i=0; 76 // sql語句實現數據庫修改 按名字修改年齡 77 String sql = "update students set Age='" + student.getAge() + "' where Name='" + student.getName() + "'"; 78 // PreparedStatement 執行動態的sql語句 如增刪改 79 PreparedStatement pstmt; 80 try { 81 pstmt = conn.prepareStatement(sql); 82 //執行動態的sql語句 包含insert update delete 83 i = pstmt.executeUpdate(); 84 if(i>0) { 85 System.out.println("修改成功!"); 86 }else { 87 System.out.println("未找到需要修改的數據!"); 88 } 89 System.out.println("restult:"+i); 90 // 關閉 91 pstmt.close(); 92 //關閉數據庫連接 93 conn.close(); 94 } catch (SQLException e) { 95 // TODO Auto-generated catch block 96 e.printStackTrace(); 97 } 98 return i; 99 } 100 //ResultSet 接口 暫存數據庫的查詢結果 101 102 //數據庫的查找 103 public static Integer getAll() { 104 // Connection 接口代表與特定的數據庫連接 並在連接上下文中執行sql 語句 105 Connection conn= getConn(); 106 String sql="select * from students"; 107 //PreparedStatement類型執行動態的sql語句 Statement 執行靜態語句 108 PreparedStatement pstmt; 109 110 try { 111 //執行sql語句 112 pstmt = conn.prepareStatement(sql); 113 // 執行給定的sql語句返回單個 ResultSet 對象 114 ResultSet rs = pstmt.executeQuery(); 115 // getMetaData() 檢索此 ResultSet對象列的數量,類型和屬性。 getColumnCount() 返回ResultSet對象列的數量 116 int col =rs.getMetaData().getColumnCount(); 117 System.out.println("============================"); 118 while(rs.next()) { 119 for(int i=1;i<=col;i++) { 120 //輸出獲取到的數據庫數據 121 System.out.print(rs.getString(i)+"\t"); 122 if((i==4)&&(rs.getString(i).length()<8)){ 123 System.out.println(); 124 } 125 } 126 //分行 127 System.out.println(); 128 } 129 System.out.println("======================"); 130 } catch (SQLException e) { 131 // TODO Auto-generated catch block 132 e.printStackTrace(); 133 } 134 return null; 135 136 } 137 138 //數據庫的刪除 139 public static int delete(String name) { 140 // Connection 接口代表與特定的數據庫連接 並在連接上下文中執行sql 語句 141 Connection conn =getConn(); 142 int i=0; 143 //修改類型的sql 語句實現 144 String sql="delete from students where Name ='"+ name+ "'"; 145 PreparedStatement pstmt; 146 147 try { 148 // 執行動態sql語句 149 pstmt = conn.prepareStatement(sql); 150 // executeUpdate() 指示受影響的行數 151 i = pstmt.executeUpdate(); 152 if(i>0) { 153 System.out.println("刪除成功!"); 154 }else { 155 System.out.println("未找到需要刪除數據!"); 156 } 157 System.out.println("result:"+i); 158 pstmt.close(); 159 conn.close(); 160 } catch (SQLException e) { 161 // TODO Auto-generated catch block 162 e.printStackTrace(); 163 } 164 165 return i; 166 } 167 168 }
Sevrelt層 實現數據庫的操作,給出界面
在控制台輸入數據時:出現輸入亂碼的問題,原因:輸入數據是為漢語輸入法,產生亂碼,所以在輸入數據的時候要注意切換成英文輸入法。
1 package com.JDBC; 2 3 import java.util.Scanner; 4 5 import com.Bean.Student; 6 import com.Dao.DAO; 7 8 public class JDBC { 9 @SuppressWarnings("resource") 10 public static void main(String[] args) { 11 String name=null; 12 String sex=null; 13 String age=null; 14 Scanner sc=null; 15 do { 16 //界面 17 System.out.println(" ~~~~~~~~~~~~~~歡迎使用數據庫~~~~~~~~~~~~~~ "); 18 System.out.println("請選擇你需要執行的操作的編號:"); 19 System.out.println("1.添加數據 2.刪除數據 3.修改數據 4.查詢數據"); 20 System.out.println("~~~~~~~~~~~~~~請輸入您的操作~~~~~~~~~~~~~~"); 21 sc=new Scanner(System.in); 22 int c=sc.nextInt(); 23 switch(c) { 24 case 1: 25 //添加數據 26 System.out.println("添加數據:"); 27 System.out.println("請輸入學生 姓名,性別,年齡:"); 28 sc=new Scanner(System.in); 29 name=sc.next(); 30 sex=sc.next(); 31 age=sc.next(); 32 DAO.insert(new Student(name,sex,age)); 33 DAO.getAll(); 34 break; 35 case 2: 36 //刪除數據 37 System.out.println("刪除數據:"); 38 System.out.println("請輸入學生 姓名:"); 39 sc=new Scanner(System.in); 40 name=sc.next(); 41 DAO.delete(name); 42 DAO.getAll(); 43 break; 44 case 3: 45 //修改數據 46 System.out.println("修改數據:"); 47 System.out.println("請輸入需要修改的同學的姓名 和 需要修改的年齡:"); 48 sc=new Scanner(System.in); 49 name=sc.next(); 50 age=sc.next(); 51 DAO.getAll(); 52 DAO.updata(new Student(name,"",age)); 53 break; 54 case 4: 55 //查詢數據 56 System.out.println("查詢數據:"); 57 DAO.getAll(); 58 break; 59 default: 60 System.out.println("錯誤!"); 61 } 62 }while(true); 63 } 64 65 }
使用Navicat Premium 的可視化數據庫展示如下: