以面向物件的思想實現資料表的新增和查詢,JDBC程式碼超詳細
阿新 • • 發佈:2018-12-04
以面向物件的思想編寫JDBC程式,實現使用java程式向資料表中新增學生資訊,並且可以實現給定身份證號查詢學生資訊或給定準考證號查詢學生資訊。
歡迎工作一到八年的Java工程師朋友們加入Java高階交流:854630135
本群提供免費的學習指導 架構資料 以及免費的解答
不懂得問題都可以在本群提出來 之後還會有直播平臺和講師直接交流噢
建立的資料表如下:
CREATE TABLE EXAMSTUDENT( FlowID INTEGER, Type INTEGER, IDCard VARCHAR(18), ExamCard VARCHAR(15), StudentName VARCHAR(20), Location VARCHAR(20), Grade INTEGER);
1.首先實現通過java程式在建立的資料表中插入一個新的student資訊,輸入詳細資訊之後顯示訊息錄入成功。
1).新建一個Student,對應examstudent資料表。
Student.java
package com.test.jdbc; public class Student { private int flowId; private int type; private String idCard; private String examCard; private String studentName; private String location; private int grade; public int getFlowId() { return flowId; } public void setFlowId(int flowId) { this.flowId = flowId; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public String getExamCard() { return examCard; } public void setExamCard(String examCard) { this.examCard = examCard; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public Student(int flowId, int type, String idCard, String examCard, String studentName, String location, int grade) { super(); this.flowId = flowId; this.type = type; this.idCard = idCard; this.examCard = examCard; this.studentName = studentName; this.location = location; this.grade = grade; } public Student(){ } @Override public String toString() { return "student [flowId=" + flowId + ", type=" + type + ", idCard=" + idCard + ", examCard=" + examCard + ", studentName=" + studentName + ", location=" + location + ", grade=" + grade + "]"; } }
2).新建一個方法:void addNewStudent(Student student),把引數Student物件插入到資料庫中。
public void addNewStudent(Student student){ //1.準備符合要求的sql語句 String sql="INSERT INTO examstudent VALUES("+student.getFlowId()+","+student.getType() +","+student.getIdCard()+","+student.getExamCard()+","+student.getStudentName()+","+ student.getLocation()+","+student.getGrade()+")"; System.out.println(sql); //2.呼叫JDBCTools類的update(sql)方法執行插入操作 JDBCTools.update(sql); }
3).從控制檯輸入學生的資訊。
private Student getStudentFromConsole() { Scanner scanner=new Scanner(System.in); Student student=new Student(); System.out.print("FlowId:"); student.setFlowId(scanner.nextInt()); System.out.print("Type:"); student.setType(scanner.nextInt()); System.out.print("IDCard:"); student.setIdCard(scanner.next()); System.out.print("ExamCard:"); student.setExamCard(scanner.next()); System.out.print("StudentName:"); student.setStudentName(scanner.next()); System.out.print("Location:"); student.setLocation(scanner.next()); System.out.print("Grade:"); student.setGrade(scanner.nextInt()); return student; }
4).方法呼叫並測試
@Test<br>public void testAddNewStudent(){ Student student=getStudentFromConsole(); addNewStudent(student); }
2.給定身份證號或准考證號查詢學生資訊
public void testGetStudent(){ //1.得到查詢的型別 int searchType=getSearchTypeFromConsole(); //2.具體查詢學生資訊 Student student=searchStudent(searchType); //3.列印學生資訊 printStudent(student); }
1).得到查詢型別
private int getSearchTypeFromConsole() { System.out.println("查詢型別:1.根據身份證號查詢學生資訊 2.根據准考證號查詢學生資訊 "); System.out.print("請輸入查詢型別:"); Scanner scanner=new Scanner(System.in); int type=scanner.nextInt(); if ((type!=1)&&(type!=2)){ System.out.print("1.根據身份證號查詢學生資訊 2.根據准考證號查詢學生資訊.請重新輸入查詢型別:"); throw new RuntimeException(); } return type; }
2).具體查詢學生資訊
private Student searchStudent(int searchType) { String sql="SELECT FlowID,TYPE,IDCard,ExamCard,StudentName,Location,Grade FROM EXAMSTUDENT WHERE "; Scanner scanner=new Scanner(System.in); if (searchType==1){ System.out.print("請輸入身份證號:"); String str=scanner.next(); sql=sql+"IDCard="+"'"+str+"'"; }else{ System.out.print("請輸入准考證號:"); String str=scanner.next(); sql=sql+"ExamCard"+"'"+str+"'"; } Student student=getStudent(sql); return student; }
資料庫操作獲取學生資訊:
private Student getStudent(String sql) { Student stu=null; Connection con=null; Statement statement=null; ResultSet resultset=null; try{ con=JDBCTools.getConnection(); statement=con.createStatement(); resultset=statement.executeQuery(sql); if(resultset.next()){ stu=new Student(resultset.getInt(1),resultset.getInt(2), resultset.getString(3),resultset.getString(4), resultset.getString(5),resultset.getString(6),resultset.getInt(7)); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCTools.release(statement, con); } return stu; }
3).列印學生資訊
private void printStudent(Student student) { if(student!=null){ System.out.println(student); }else{ System.out.println("查無此人"); } }
完整的示例程式碼:
Student.java(上面已給出)
資料庫操作工具類:JDBCTools.java
package com.test.jdbc; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.junit.Test; public class JDBCTools { //更新資料庫 public static void update(String sql){ Connection con=null; Statement statement=null; try{ con=JDBCTools.getConnection(); statement=con.createStatement(); statement.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); }finally{ JDBCTools.release(statement, con); } } //獲取資料庫的連線 public static Connection getConnection() throws Exception{ String driverClass=null; String jdbcUrl=null; String user=null; String password=null; InputStream in=JDBCTools.class.getResourceAsStream("/jdbc.properties"); Properties properties=new Properties(); properties.load(in); driverClass=properties.getProperty("driver"); jdbcUrl=properties.getProperty("jdbcUrl"); user=properties.getProperty("user"); password=properties.getProperty("password"); Class.forName(driverClass); Connection connection=DriverManager.getConnection(jdbcUrl,user,password); return connection; } @Test public void testGetConnection() throws Exception{ getConnection(); } //資料庫釋放 public static void release(Statement statement,Connection connection){ if(statement!=null){ try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
功能實現類:JDBCTest.java
package com.test.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; import org.junit.Test; public class JDBCTest { @Test public void testGetStudent(){ //1.得到查詢的型別 int searchType=getSearchTypeFromConsole(); //2.具體查詢學生資訊 Student student=searchStudent(searchType); //3.列印學生資訊 printStudent(student); } private void printStudent(Student student) { if(student!=null){ System.out.println(student); }else{ System.out.println("查無此人"); } } private Student searchStudent(int searchType) { String sql="SELECT FlowID,TYPE,IDCard,ExamCard,StudentName,Location,Grade FROM EXAMSTUDENT WHERE "; Scanner scanner=new Scanner(System.in); if (searchType==1){ System.out.print("請輸入身份證號:"); String str=scanner.next(); sql=sql+"IDCard="+"'"+str+"'"; }else{ System.out.print("請輸入准考證號:"); String str=scanner.next(); sql=sql+"ExamCard"+"'"+str+"'"; } Student student=getStudent(sql); return student; } private Student getStudent(String sql) { Student stu=null; Connection con=null; Statement statement=null; ResultSet resultset=null; try{ con=JDBCTools.getConnection(); statement=con.createStatement(); resultset=statement.executeQuery(sql); if(resultset.next()){ stu=new Student(resultset.getInt(1),resultset.getInt(2), resultset.getString(3),resultset.getString(4), resultset.getString(5),resultset.getString(6),resultset.getInt(7)); } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCTools.release(statement, con); } return stu; } private int getSearchTypeFromConsole() { System.out.println("查詢型別:1.根據身份證號查詢學生資訊 2.根據准考證號查詢學生資訊 "); System.out.print("請輸入查詢型別:"); Scanner scanner=new Scanner(System.in); int type=scanner.nextInt(); if ((type!=1)&&(type!=2)){ System.out.print("1.根據身份證號查詢學生資訊 2.根據准考證號查詢學生資訊.請重新輸入查詢型別:"); throw new RuntimeException(); } return type; } @Test public void testAddNewStudent(){ Student student=getStudentFromConsole(); addNewStudent(student); } //從控制檯輸入學生的資訊 private Student getStudentFromConsole() { Scanner scanner=new Scanner(System.in); Student student=new Student(); System.out.print("FlowId:"); student.setFlowId(scanner.nextInt()); System.out.print("Type:"); student.setType(scanner.nextInt()); System.out.print("IDCard:"); student.setIdCard(scanner.next()); System.out.print("ExamCard:"); student.setExamCard(scanner.next()); System.out.print("StudentName:"); student.setStudentName(scanner.next()); System.out.print("Location:"); student.setLocation(scanner.next()); System.out.print("Grade:"); student.setGrade(scanner.nextInt()); return student; } public void addNewStudent(Student student){ //1.準備符合要求的sql語句 String sql="INSERT INTO examstudent VALUES("+student.getFlowId()+","+student.getType() +","+student.getIdCard()+","+student.getExamCard()+","+student.getStudentName()+","+ student.getLocation()+","+student.getGrade()+")"; System.out.println(sql); //2.呼叫JDBCTools類的update(sql)方法執行插入操作 JDBCTools.update(sql); } }