JDBC: Writing JDBC programs with object-oriented thinking

Source: Internet
Author: User
Tags ticket

Topic Description:The student information is added to the data table, and the student's information can be queried through the ID number of the ticket. Name Type Nullable Default Comments
----------- ------------ -------- ------- --------
Idcard Number (Ten) Y
Examcard Number (Ten) Y
Studentname VARCHAR2 (Y)
Lacation VARCHAR2 (Y)
GRADE Number (3) Y

Test Class
Package Xuezaipiao3;import Java.sql.connection;import Java.sql.resultset;import java.sql.statement;import Java.util.scanner;import Xuezaipiao1. jdbc_tools;/** * Think: * Add a student information record to the datasheet, then the student information can create a student class to store the information * Step: * 1) Student member variable corresponding STUDNT data table * 2) Create a method Addstudent (Stu Dent Student) * 3) Execute the corresponding SQL operation in method * @author Kevy * */public class Thinkinjdbc {public static void main (string[] args) {S Tudent s = getstudentfromconsole (); Addstudent (s); Querystudent ();} public static void Querystudent () {int searchtype = Getsearchtypefromconsole (); Student Student = searchstudent (searchtype);p rintstudent (Student);} /** * Print Student information * @param student */private static void Printstudent (student student) {if (student!=null) {System.out.println (s tudent);} Else{system.out.println ("Check no this person!");}} /** * * @param searchtype 1 and 2 * @return */private static Student searchstudent (int searchtype) {String sql = "Select * FROM student "+" where "; Scanner Scanner = new Scanner (system.in), if (searchtype==1) {System.out.print ("Please enter identity card:"); inT id = scanner.nextint (); sql = sql + "Idcard =" +id;} Else{system.out.print ("Please enter admission ticket:"); int id = scanner.nextint (); sql = sql + "Examcard =" +id;} Student Student = getstudent (sql); Scanner.close (); return Student;} /** * Returns Student object based on incoming SQL * @param SQL * @return */private static Student getstudent (String sql) {Connection Connection = Null Statement Statement = null; ResultSet rs = null; Student stu = null;try {connection = jdbc_tools.getconnection (); statement = Connection.createstatement (); rs = Statement.executequery (SQL), if (Rs.next ()) {stu = new Student (Rs.getint ("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 with ID card query, 2 with the ticket number to query other invalid */@SuppressWarnings ("resource") private static int getsearchtypefromconsole () { System.out.println ("Please enter query type: 1. ID Card Enquiry 2. Admission Ticket Enquiry"); System.out.print ("Your choice:"); Scanner Scanner = New Scanner (system.in), int type = Scanner.nextint (), if (type!=1 && type!=2) {System.out.println ("input wrong, please reenter"); throw new RuntimeException ();} Scanner.close (); return 0;} /** * Get information from the console and create student objects * @return */private static Student getstudentfromconsole () {Scanner Scanner = new Scanner (system.in ); Student Student = new Student (); System.out.print ("Idcard:"); Student.setidcard (Scanner.nextint ()); System.out.print ("Examid:"); Student.setexamid (Scanner.nextint ()); System.out.print ("Studentname:"); Student.setstudentname (Scanner.next ()); System.out.print ("llocation:"); Student.setlacation (Scanner.next ()); System.out.print ("Grade:"); Student.setgrade (Scanner.nextint ()); Scanner.close (); return student;} /** * Add student information * @param student */public static void Addstudent (student student) {String sql = "INSERT into student" + "VALUE S ("+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=" + Studentna Me + ", 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 class
Package xuezaipiao1;/** * JDBC Tool class * Encapsulates some simple JDBC operation method * Version 1 */import java.io.fileinputstream;import java.io.FileNotFo Undexception;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, DELETE, does not contain a select * parameter String SQL statement * @return Int executed several records */public static int update (String sql) {Connection conn = null; Statement Statement = Null;int num = 0;try {try {conn = Jdbc_tools.getconnection ();} catch (Exception e) {E.printstacktrac E ();} statement = conn.createstatement (); num = statement.executeupdate (sql);} catch (SQLException e) {e.printstacktrace ();} FINALLY{JDBC_TOOLS.RELASESOURCE (conn, statement);} return num;} /** * The 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.executequery (sql); while (Rs.next ()) {System.out.println (Rs.getint ("id")); System.out.println (rs.getstring (2)); System.out.println (rs.getstring ("email"));}} catch (Exception e) {e.printstacktrace ();} Finally{jdbc_tools.relasesource (RS, conn, statement);}} /** * Used to release resources, parameters are Connection, Statement * @param conn * @param Statement */public static void Relasesource (ResultSet Rs,co Nnection 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, so that if an exception occurs in the middle, the program continues execution 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);p roperties.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;}}




JDBC: Writing JDBC programs with object-oriented thinking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.