JDBC實例java
package jdbc2; public class ExamStudent { int flowid; int type; String idcard; String examCard; String studentname; String location; int grade; public ExamStudent() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "ExamStudent [flowid=" + flowid + ", type=" + type + ", idcard=" + idcard + ", examCard=" + examCard + ", studentname=" + studentname + ", location=" + location + ", grade=" + 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 ExamStudent(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 static Connection getConnection() throws Exception{ String driverClass = null; String url = null; String user = null; String password = null; //讀取properties配置文件 InputStream is = TestJDBC.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); //加載數據庫驅動(對應的Driver實現類中有註冊驅動的靜態代碼塊) Class.forName(driverClass); Properties info = new Properties(); info.put("user", user); info.put("password", password); //經過DriverManager的getConnection()方法獲取數據庫鏈接 return DriverManager.getConnection(url, user, password); }
二、釋放資源封裝的方法
this
//釋放資源 public static void release(Statement statement,Connection conn,ResultSet rs){ if(rs != null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(statement!=null){ try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
三、簡單的學生登陸封裝的方法url
//登陸 private int getSearchTypeFormConsole(){ System.out.println("請輸入登陸類型:1:身份證登陸 2.准考證號登錄"); Scanner sc = new Scanner(System.in); int type = sc.nextInt(); if(type != 1 && type != 2){ throw new RuntimeException("輸入類型有誤"); } return type; }
四、建立查詢語句spa
public String SearchStudent(int type){ ExamStudent es = new ExamStudent(); Scanner sc = new Scanner(System.in); String sql = "select * from ExamStudent where idcard = " ; if(type == 1){ System.out.println("請輸入身份證號"); String idcard = sc.next(); es.setIdcard(idcard); sql = sql + es.getIdcard(); }else{ System.out.println("請輸入准考證號"); String examcard = sc.next(); es.setExamCard(examcard); sql = sql + es.getExamCard(); } return sql; }
五、查詢學生rest
public ExamStudent getStudent(String sql){ Connection conn = null; Statement statement = null; ResultSet rs = null; ExamStudent es = null; try { conn = getConnection(); statement = conn.createStatement(); rs = statement.executeQuery(sql); while(rs.next()){ es = new ExamStudent( rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getInt(7)); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ release(statement, conn, rs); } return es; }
六、輸出學生code
public void printStudent(ExamStudent es){ if(es !=null){ System.out.println(es); }else{ System.out.println("查無此人!!!"); } }
七、測試
@Test public void test1() throws Exception{ //1.登陸 int type = getSearchTypeFormConsole(); //獲取信息 String sql = SearchStudent(type); //查詢 ExamStudent es = getStudent(sql); //輸出 printStudent(es); }
八、從控制檯輸入ExamStudent對象
//從控制檯輸入ExamStudent對象 public ExamStudent getExamStudentFromConsole(){ ExamStudent es = new ExamStudent(); Scanner sc = new Scanner(System.in); System.out.println("學生號: "); es.flowid = sc.nextInt(); System.out.println("考試類型: "); es.type = sc.nextInt(); System.out.println("身份證: "); es.idcard = sc.next(); System.out.println("考號: "); es.examCard = sc.next(); System.out.println("學生名: "); es.studentname = sc.next(); System.out.println("學生地址: "); es.location = sc.next(); System.out.println("學生班級: "); es.grade = sc.nextInt(); return es; }
九、封裝的sql語句修改、插入、刪除方法
public void update(String sql){ Connection conn = null; Statement statement = null; ResultSet rs = null; try { conn = getConnection(); statement = conn.createStatement(); statement.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ TestJDBC.release(statement, conn,rs); } }
十、添加學生
//添加學生到數據庫 public void addStudent(){ ExamStudent stu = getExamStudentFromConsole(); String sql = "insert into examstudent values(" + stu.getFlowid() +"," + stu.getType() + ",'" + stu.getIdcard() + "','" + stu.getExamCard() + "','" + stu.getStudentname() + "','" + stu.getLocation() + "'," + stu.getGrade() + ")"; update(sql); System.out.println("插入成功!!!!"); }
一、從控制檯獲得須要插入的學生信息
//從控制檯輸入ExamStudent對象 public ExamStudent getExamStudentFromConsole(){ ExamStudent es = new ExamStudent(); Scanner sc = new Scanner(System.in); System.out.println("學生號: "); es.flowid = sc.nextInt(); System.out.println("考試類型: "); es.type = sc.nextInt(); System.out.println("身份證: "); es.idcard = sc.next(); System.out.println("考號: "); es.examCard = sc.next(); System.out.println("學生名: "); es.studentname = sc.next(); System.out.println("學生地址: "); es.location = sc.next(); System.out.println("學生班級: "); es.grade = sc.nextInt(); return es; }
二、獲取數據庫鏈接
//獲取數據庫鏈接方法 public static Connection getConnection() throws Exception{ String driverClass = null; String url = null; String user = null; String password = null; //讀取properties配置文件 InputStream is = TestJDBC.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(is); driverClass = properties.getProperty("driverClass"); url = properties.getProperty("url"); user = properties.getProperty("user"); password = properties.getProperty("password"); //加載數據庫驅動(對應的Driver實現類中有註冊驅動的靜態代碼塊) Class.forName(driverClass); Properties info = new Properties(); info.put("user", user); info.put("password", password); //經過DriverManager的getConnection()方法獲取數據庫鏈接 return DriverManager.getConnection(url, user, password); }
三、經過prestatement進行數據插入
//重寫update方法 public void update(String sql,Object...args){ Connection conn = null; PreparedStatement preparedStatement = null; try { conn = getConnection(); preparedStatement = conn.prepareStatement(sql); for(int i=0;i<args.length;i++){ preparedStatement.setObject(i+1, args[i]); } int result = preparedStatement.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
四、添加學生
//添加學生 public void addStudent2(ExamStudent es){ String sql = "insert into examstudent(flowid,type,idcard,examcard,studentname,location,grade) values(?,?,?,?,?,?,?)"; update(sql,es.getFlowid(),es.getType(),es.getIdcard(),es.getExamCard(),es.getStudentname(),es.getLocation(),es.getGrade()); }
五、進行測試
@Test public void test3(){ ExamStudent es = getExamStudentFromConsole(); addStudent2(es); }