JDBC Additions and deletions (using configuration files)

Source: Internet
Author: User

Jdbcdemo2.java

Package Com.zhangbz.jdbc;import Java.sql.connection;import java.sql.resultset;import java.sql.statement;import Org.junit.test;import com.zhangbz.util.JDBCUtils; Public classJDBCDemo2 {@Test Public voidDelete () {Connection conn=NULL; Statement Stat=NULL; Try{conn=Jdbcutils.getconn (); Stat=conn.createstatement (); Stat.executeupdate ("Delete from user where name = ' Zhaoliu '"); } Catch(Exception e) {e.printstacktrace (); } finally{jdbcutils.close (NULL, STAT, conn); }} @Test Public voidfind () {Connection conn=NULL; Statement Stat=NULL; ResultSet RS=NULL; Try{conn=Jdbcutils.getconn (); Stat=conn.createstatement (); RS= Stat.executequery ("select* from user where name = ' Zhaoliu '");  while(Rs.next ()) {String name= Rs.getstring ("name"); String Password= Rs.getstring ("Password"); System. out. println (name +":"+password); }        } Catch(Exception e) {e.printstacktrace (); } finally{jdbcutils.close (RS, stat, conn); }} @Test Public voidUpdate () {Connection conn=NULL; Statement Stat=NULL; Try{conn=Jdbcutils.getconn (); Stat=conn.createstatement (); Stat.executeupdate ("Update user set password=999 where name = ' Zhaoliu '"); } Catch(Exception e) {e.printstacktrace (); } finally{jdbcutils.close (NULL, STAT, conn); }} @Test Public voidAdd () {Connection conn=NULL; Statement Stat=NULL; Try {            //1. Registering the database driver//2. Get the connectionconn =Jdbcutils.getconn (); //3. Get the Transmitter objectStat =conn.createstatement (); //4. Execute SQL statements            intCount = Stat.executeupdate ("INSERT INTO user values (null, ' Zhaoliu ', ' 123456 ', ' [email protected] ', ' 1999-09-09 ')"); //5. Processing Results            if(Count >0) {System. out. println ("Successful Execution! The number of rows affected is"+count); } Else{System. out. println ("Execution failed!! "); }        } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            //6. Close ResourcesJdbcutils.close (NULL, STAT, conn); }    }}

Jdbcutils.java

1  PackageCom.zhangbz.util;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 Importjava.util.Properties;9 Ten  Public classJdbcutils { One      Public StaticProperties prop =NULL; A     Privatejdbcutils () { -     } -     //using configuration Files the     Static { -         Try { -Prop =NewProperties (); -Prop.load (jdbcutils.class. getClassLoader (). getResourceAsStream ("Config.properties")); +}Catch(Exception e) { - e.printstacktrace (); +             Throw NewRuntimeException (e); A         } at     } -     /** - * Get Connections -      * @return -      * @throwsclassnotfoundexception -      * @throwsSQLException in      */ -      Public StaticConnection Getconn ()throwsClassNotFoundException, SQLException { to         //1. Registering the database driver +Class.forName (Prop.getproperty ("Driver")); -         //2. Get the connection the         returnDrivermanager.getconnection (prop.getproperty ("url"), Prop.getproperty ("user"), Prop.getproperty ("Password")); *     } $     /**Panax Notoginseng * Close Connection -      */ the      Public Static voidClose (ResultSet RS, Statement Stat, Connection conn) { +         if(rs! =NULL) { A             Try { the rs.close (); +}Catch(SQLException e) { -                 //TODO auto-generated Catch block $ e.printstacktrace (); $}finally { -rs =NULL; -             } the         } -         if(Stat! =NULL) {Wuyi             Try { the stat.close (); -}Catch(SQLException e) { Wu                 //TODO auto-generated Catch block - e.printstacktrace (); About}finally { $Stat =NULL; -             } -         } -         if(Conn! =NULL) { A             Try { + conn.close (); the}Catch(SQLException e) { -                 //TODO auto-generated Catch block $ e.printstacktrace (); the}finally { theconn =NULL; the             } the         } -     } in}

Config.properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql:///day10user=Rootpassword =root

JDBC Additions and deletions (using configuration files)

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.