Java-Object oriented thinking for JDBC programming

Source: Internet
Author: User

Mysql-connector-java-5.1.7-bin.jar jdbc.properties
driver=com.mysql.jdbc.driverjdbcurl=jdbc\:mysql\://localhost\:3306/lessonuser=rootpassword=
Jdbctools.java
The package object-oriented concept for JDBC programming; import Java.io.ioexception;import Java.io.inputstream;import Java.sql.connection;import Java.sql.sqlexception;import Java.util.properties;import Com.mysql.jdbc.driver;import com.mysql.jdbc.Statement; public class Jdbctools {public static void update (String sql) {Connection conn=null;java.sql.statement statement=null; try {conn=getconnection (); statement= conn.createstatement (); statement.executeupdate (sql);} catch (Exception e) {// TODO auto-generated catch Blocke.printstacktrace ();} Finally{jdbctools.release (conn, (Statement) Statement);}} public static Connection getconnection () throws Instantiationexception, Illegalaccessexception, ClassNotFoundException, SQLException, ioexception{string driverclass=null; String Jdbcurl=null; String User=null; String password=null;inputstream in= JDBCTools.class.getClassLoader (). getResourceAsStream ("jdbc.properties"); Properties P=new properties ();p. Load (in);d riverclass=p.getproperty ("Driver"); Jdbcurl=p.getproperty ("Jdbcurl"); User=p.getpRoperty ("user");p assword=p.getproperty ("password");D River Driver= (Driver) class.forname (driverclass). newinstance (); Properties Info=new properties (); Info.put ("User", user), Info.put ("password", password); Connection Connection=driver.connect (Jdbcurl, info); System.out.println (connection); return connection;} public static void Release (Connection conn,statement Statement) {try {if (statement!=null) Statement.close ();} catch ( Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{if (Conn!=null) try {conn.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace () ;}}}}
Student.java
public class Student {private int id;private string name;private int age;private int clas;private string Address;private S Tring Sex;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public int Getclas () {return clas;} public void Setclas (int clas) {this.clas = Clas;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} public Student (int ID, string name, int age, int clas, string address,string sex) {super (); this.id = Id;this.name = Name;t His.age = Age;this.clas = clas;this.address = Address;this.sex = sex;} Public Student () {super ();} @Overridepublic String toString () {return "Student [id=" + ID + ", name=" + name + ", age=" + age+ ", clas=" + Clas + ", a Ddress= "+Address + ", sex=" + sex+ "]";}} 
Test.java
Import Java.io.ioexception;import Java.sql.sqlexception;import Java.util.scanner;public class Test {//public void Testaddnewstudent () throws Instantiationexception, Illegalaccessexception, ClassNotFoundException, SQLException, Ioexception//{////testaddnew (Test.getinformationfromconsole ());//}private Static Student Getinformationfromconsole () {Scanner sc=new Scanner (system.in); Student stu=new Student (); System.out.println ("Id:"); Stu.setid (Sc.nextint ()); System.out.println ("Name:"); Stu.setname (Sc.next ()); System.out.println ("Age:"); Stu.setage (Sc.nextint ()); System.out.println ("Clas:"); Stu.setclas (Sc.nextint ()); System.out.println ("Address:"); Stu.setaddress (Sc.next ()); System.out.println ("Sex:"); Stu.setsex (Sc.next ()); return Stu;} public void Testaddnew (Student stu) throws Instantiationexception, Illegalaccessexception, ClassNotFoundException, SQLException, ioexception{string sql= "INSERT into study VALUES (" +stu.getid () + ", '" +stu.getname () + "'," +stu.getage () + " , "+stu.getclas () +", ' "+stu.getaddress () +" ', ' "+stu.getsex () +" ') "; SYSTEM.OUT.PRINTLN (SQL);    Jdbctools.update (SQL); }public static void Main (string[] args) {test te=new Test (); try {te.testaddnew (Te.getinformationfromconsole ());} catch ( Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (SQLException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Java-Object oriented thinking for JDBC programming

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.