JAVA-JDBC Tool Class Jdbcutil

Source: Internet
Author: User

Create a configuration file under the SRC folder db.properties

Db.properties

Drivername=com.mysql.jdbc.driverurl=jdbc:mysql://localhost:3306/mydbuser=rootpassword=root

  

Jdbcutil.java

Package Com.gordon.jdbcutil;import Java.io.inputstream;import Java.sql.connection;import Java.sql.DriverManager; Import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Java.util.properties;public class Jdbcutil {public static string drivername = Null;public static string URL = Null;public static string USER = Null;public static string PASSWORD = Null;public static Connection conn = null;static {try {Propertie s props = new Properties (),//reader in = new FileReader ("Db.properties"), InputStream in = JDBCUtil.class.getClassLoader () . getResourceAsStream ("Db.properties");p rops.load (in);D rivername = Props.getproperty ("drivername"); url = props.getproperty ("url"); user = Props.getproperty ("user"); PASSWORD = Props.getproperty ("PASSWORD");} catch (Exception e) {throw new RuntimeException (e);}} public static Connection getconnection () throws Exception {if (conn! = null) {return conn;} Class.forName (drivername); conn = Drivermanager.getconnection (URL, USER, PasswoRD); return conn;} public static void Closeresource (Connection conn, PreparedStatement St) throws SQLException {st.close (); Conn.close ();} public static void Closeresource (Connection conn, ResultSet RS, PreparedStatement St) throws SQLException {St.close (), Rs. Close (); Conn.close ();}}

  * Note the code in which to get the configuration file.

Jdbc01.java test File

Package Com.gordon.jdbc;import Java.sql.connection;import Java.sql.preparedstatement;import java.sql.ResultSet; Import Java.sql.sqlexception;import Com.gordon.jdbcutil.jdbcutil;public class Jdbc01 {public static void main (string[] args) {selectfunction ();} public static void Selectfunction () {try {//1. Get connection Connection conn = Jdbcutil.getconnection ();//3.sql statement String sql = "Selec T * from user ";//3. Get SQL performer PreparedStatement st = conn.preparestatement (SQL);//4. Set parameters//5. Execute SQL statement ResultSet rs = St.execu Tequery ();//6. Processing data while (Rs.next ()) {System.out.println (rs.getstring ("id") + ":" + rs.getstring ("name"));} 7. Release Resources Jdbcutil.closeresource (conn, RS, st);} catch (SQLException ex) {System.out.println (Ex.getmessage ());} catch (Exception e) {System.out.println (E.getmessage () );}}}

  

JAVA-JDBC Tool Class Jdbcutil

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.