JDBC: compile a JDBC program with an object-oriented approach
Brief Introduction:Add student information to the data table, and query student information through the ID card number admission ticket.Name Type Nullable Default Comments
----------------------------------------------
Idcard number (10) Y
Examcard number (10) Y
STUDENTNAME VARCHAR2 (20) Y
LACATION VARCHAR2 (20) Y
Grade number (3) Y
Test class
Package xuezaipiao3; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. statement; import java. util. metadata; import xuezaipiao1.JDBC _ Tools;/*** thinking: * Add a student information record to the data table, so that student information can be created to store information. * Step: * 1) student member variable corresponds to studnt data table * 2) create a method addStudent (Student student) * 3) execute the corresponding SQL operation * @ author Kevy **/public class thinkInJDBC {public static void main (String [] args) {Student s = getSt UdentFromConsole (); addStudent (s); QueryStudent ();} public static void QueryStudent () {int searchType = getSearchTypeFromConsole (); Student student = searchStudent (searchType ); printStudent (student);}/** print student information * @ param Student */private static void printStudent (student) {if (student! = Null) {System. out. println (student);} else {System. out. println ("No such person found! ") ;}}/***** @ Param searchType 1 And 2 * @ return */private static Student searchStudent (int searchType) {String SQL = "SELECT * FROM student" + "where"; limit = new limit (System. in); if (searchType = 1) {System. out. print ("Enter your id card:"); int id = domestic. nextInt (); SQL = SQL + "IDCARD =" + id;} else {System. out. print ("Enter Admission Ticket:"); int id = admission. nextInt (); SQL = SQL + "EXAMCARD =" + id;} Student studen T = getStudent (SQL); student. close (); return student;}/*** return the Student object * @ param SQL * @ return */private static Student getStudent (String SQL) based on the input SQL statement) {Connection connection = null; Statement statement = null; ResultSet rs = null; Student stu = null; try {connection = JDBC_Tools.getConnection (); statement = connection. createStatement (); rs = statement.exe cuteQuery (SQL); if (rs. next () {stu = new Student (rs. g EtInt ("IDCARD"), rs. getInt ("EXAMCARD"), rs. getString ("STUDENTNAME"), rs. getString ("LACATION"), rs. getInt ("GRADE");} catch (Exception e) {e. printStackTrace ();} finally {JDBC_Tools.relaseSource (rs, connection, statement);} return stu;}/***** @ return 1 query with an ID card, 2 Other invalid queries with admission ticket No. */@ SuppressWarnings ("resource") private static int getSearchTypeFromConsole () {System. out. println ("Enter the query type: 1. ID card query 2. admission Ticket query "); System. Out. print ("your choice:"); lost bytes = new bytes (System. in); int type = gradient. nextInt (); if (type! = 1 & type! = 2) {System. out. println ("incorrect input, please input again"); throw new RuntimeException ();} timeout. close (); return 0;}/*** obtain information from the console and create a Student object * @ return */private static Student getStudentFromConsole () {Student metadata = new Student (System. in); Student student = new Student (); System. out. print ("IDCard:"); student. setIDCard (response. nextInt (); System. out. print ("ExamID:"); student. setExamID (response. nextInt (); System. out. print ("StudentName:"); student. setStudentName (response. next (); System. out. print ("Llocation:"); student. setLacation (signature. next (); System. out. print ("Grade:"); student. setGrade (gradient. nextInt (); substring. close (); return student;}/*** add student information * @ param Student */public static void addStudent (student) {String SQL = "INSERT INTO STUDENT" + "VALUES (" + student. getIDCard () + "," + student. getExamID () + ", '" + student. getStudentName () + "','" + student. getLacation () + "'," + student. getGrade () + ")"; JDBC_Tools.update (SQL );}}
Student Class
public class Student {private int IDCard;private int ExamID;private String StudentName;private String Lacation;private int Grade;public Student(int iDCard, int examID, String studentName, String lacation,int grade) {super();IDCard = iDCard;ExamID = examID;StudentName = studentName;Lacation = lacation;Grade = grade;}@Overridepublic String toString() {return "Student [IDCard=" + IDCard + ", ExamID=" + ExamID+ ", StudentName=" + StudentName + ", Lacation=" + Lacation+ ", Grade=" + Grade + "]";}public Student() {super();}public int getIDCard() {return IDCard;}public void setIDCard(int iDCard) {IDCard = iDCard;}public int getExamID() {return ExamID;}public void setExamID(int examID) {ExamID = examID;}public String getStudentName() {return StudentName;}public void setStudentName(String studentName) {StudentName = studentName;}public String getLacation() {return Lacation;}public void setLacation(String lacation) {Lacation = lacation;}public double getGrade() {return Grade;}public void setGrade(int grade) {Grade = grade;}}
JDBC Tool
Package xuezaipiao1;/*** JDBC tool class * encapsulates some simple JDBC operation methods * version 1 */import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. properties; public class JDBC_Tools {/*** method used to execute SQL, including INSRT, UPDATE, and DELETE, excluding S ELECT * parameter String SQL Statement * @ return int executes several records */public static int update (String SQL) {Connection conn = null; statement Statement = null; int num = 0; try {conn = JDBC_Tools.getConnection ();} catch (Exception e) {e. printStackTrace ();} statement = conn. createStatement (); num = statement.exe cuteUpdate (SQL);} catch (SQLException e) {e. printStackTrace ();} finally {JDBC_Tools.relaseSource (conn, statement) ;} Return num;}/*** SELECT method used to execute SQL */public static void query (String SQL) {Connection conn = null; Statement statement = null; resultSet rs = null; try {conn = JDBC_Tools.getConnection (); statement = conn. createStatement (); rs = statement.exe cuteQuery (SQL); while (rs. next () {System. out. println (rs. getInt ("id"); System. out. println (rs. getString (2); System. out. println (rs. getString ("email");} catch (Exc Eption e) {e. printStackTrace ();} finally {JDBC_Tools.relaseSource (rs, conn, statement) ;}/ *** used to release resources, the parameters are Connection, Statement * @ param conn * @ param statement */public static void relaseSource (ResultSet rs, Connection conn, Statement statement) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} JDBC_Tools.relaseSource (conn, statement);} public static void relaseSource (Connection conn, Statement statement) {if (statement! = Null) {try {statement. close ();} catch (SQLException e) {e. printStackTrace () ;}// use two if statements, so that the program continues to run if (conn! = Null) {try {conn. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ ***** @ return * @ throws Exception */public static Connection getConnection () throws Exception {Properties properties = new Properties (); try {// InputStream in = getClass (). getClassLoader (). getResourceAsStream ("jdbc. properties "); // properties. load (in); properties. load (new FileInputStream ("D: // LearnJava // learnJDBC // Lesson2_UseStatementAndResultSet // src // jdbc. properties ");} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} String user = properties. getProperty ("user"); String password = properties. getProperty ("password"); String jdbcUrl = properties. getProperty ("jdbcUrl"); String dirverName = properties. getProperty ("driver"); try {Class. forName (dirverName);} catch (ClassNotFoundException e1) {e1.printStackTrace ();} Connection connection = null; try {connection = DriverManager. getConnection (jdbcUrl, user, password);} catch (SQLException e) {e. printStackTrace ();} return connection ;}}