Using MySQL (6-part song)

Source: Internet
Author: User

Use the properties file to configure the database driver, URI, user name and password, so as to make it easier to change the database later without changing the source code;

driver=Com.mysql.jdbc.Driveruri=jdbc:mysql://localhost:3306/day10user=  Rootpassword=root

1. Registration Driver

 Public Static throws FileNotFoundException, IOException, ClassNotFoundException, sqlexception{2         3         Class.forName (Prop.getproperty ("Driver")); 4         return Drivermanager.getconnection (Prop.getproperty ("uri"), Prop.getproperty ("user"), Prop.getproperty ("Password") ); 5         6         7     }

2. Create a connector

Connection conn = jdbcutils.getconn (); // Connection conn = Drivermananger ("Uri,user,password");

3. Get the transmitter

statment stat = conn.createstatement ();

5. Query the statement with the transmitter and return the result set

Stat.executeupdate (SQL); Delete and change

Stat.executequery (SQL); Query statement

6. Close Resources

1  Public Static voidClose (Connection conn,statement Stat,resultset rs) {2         3             if(rs!=NULL){4                 Try {5 rs.close ();6}Catch(SQLException e) {7                     Throw NewRuntimeException (e);8                     9}finally{Tenrs=NULL; One                 } A             } -             if(stat!=NULL){ -                 Try { the stat.close (); -}Catch(SQLException e) { -                     Throw NewRuntimeException (e); -                      +}finally{ -stat=NULL; +                 } A             } at             if(conn!=NULL){ -                 Try { - conn.close (); -}Catch(SQLException e) { -                     Throw NewRuntimeException (e); -                      in}finally{ -conn=NULL; to                 } +             } -}

Using MySQL (6-part song)

Related Article

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.