JAVA_DBC Connection Database _ How to read a configuration file

Source: Internet
Author: User

1  PackageCom.homewoek3_4.dao;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.sql.Connection;6 ImportJava.sql.DriverManager;7 Importjava.sql.PreparedStatement;8 ImportJava.sql.ResultSet;9 Importjava.sql.SQLException;Ten Importjava.util.Properties; One  A /** - * Database connection general class -  * @authorAdministrator the  */ -  Public Abstract classBasedao { -     Private Static FinalString Path = "Database.properties"; -     Private StaticString Db_driver; +     Private StaticString Db_url; -     Private StaticString Db_user; +     Private StaticString db_pwd; A      at     protectedConnection conn =NULL; -     protectedPreparedStatement PS =NULL; -     protectedResultSet rs =NULL; -      -     Static { -Properties Pro =NewProperties (); inInputStream io = Basedao.class. getClassLoader (). getResourceAsStream (path); -         Try { to             //reading configuration Files + pro.load (IO); -}Catch(IOException e) { the e.printstacktrace (); *         } $Db_driver = Pro.getproperty ("Db_driver");Panax NotoginsengDb_url = Pro.getproperty ("Db_url"); -Db_user = Pro.getproperty ("Db_user"); theDb_pwd = Pro.getproperty ("Db_pwd"); +         Try { A             //Load Driver class the Class.forName (db_driver); +}Catch(ClassNotFoundException e) { - e.printstacktrace (); $         } $     } -     /** - * Open Database connection the      */ -     protected voidOpenconn () {Wuyi         Try { theconn =drivermanager.getconnection (Db_url, Db_user, db_pwd); -}Catch(SQLException e) { Wu e.printstacktrace (); -         } About     } $     /** - * Close Database connection -      */ -     protected voidCloseconn () { A         Try { +             if(rs! =NULL) { the rs.close (); -             } $             if(PS! =NULL) { the ps.close (); the             } the             if(Conn! =NULL) { the conn.close (); -             } in}Catch(SQLException e) { the e.printstacktrace (); the         } About     } the     /** the * Adding and deleting changes the      * @paramSQL +      * @paramobj -      * @return the      */Bayi     protected intexecuteupdate (String sql, object...obj) { the         intresult =-1; the          This. Openconn (); -         Try { -PS =conn.preparestatement (SQL); the             if(obj! =NULL) { the                  for(inti = 0; i < obj.length; i++) { thePs.setobject (i+1, Obj[i]); the                 } -             } theresult =ps.executeupdate (); the}Catch(SQLException e) { the e.printstacktrace ();94         } the         returnresult; the     } the     /**98 * Enquiry About      * @paramSQL -      * @paramobj101      * @return102      */103     protected voidexecuteQuery (String sql, object...obj) {104          This. Openconn (); the         Try {106PS =conn.preparestatement (SQL);107             if(obj! =NULL) {108                  for(inti = 0; i < obj.length; i++) {109Ps.setobject (i+1, Obj[i]); the                 }111             } thers =ps.executequery ();113}Catch(SQLException e) { the e.printstacktrace (); the         } the     }117}

Create a database.properties file, note: The suffix name must be properties. This format of the file in the MyEclipse icon will change, this is easy to write wrong, be careful.

1 Db_driver=com.mysql.jdbc.driver 2 db_url=jdbc:mysql://localhost:3306/pet?useunicode=true&characterencoding=utf8& UseSSL=true3db_user=root4 db_pwd=111111

JAVA_DBC Connection Database _ How to read a configuration file

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.