Encapsulating the JDBC Tool class

Source: Internet
Author: User

The basic steps of the JDBC connection database are fixed, so you can consider encapsulating a tool class to simplify database operations.

The use of the properties of Java in the package configuration file, is a key-value pair in the form of the existence, you can connect the database to dynamic information to the inside, so it is more intuitive, not error-prone, and easy to maintain.

Put the configuration file under SRC, if you want to put it under the package, the relative path of the configuration file must start with the package name.

Demo:

Db.properties

mysqldriver=com.mysql.jdbc.drivermysqlurl=jdbc:mysql://localhost:3306/student?useunicode=true& Characterencoding=utf8mysqluser=rootmysqlpwd=1234oracledriver=oracle.jdbc.driver.oracledriveroracleurl=jdbc\:o Racle\:thin\: @localhost \:1521\:orcloracleuser=scottoraclepwd=tiger

Jdbcutil.java

Package Com.wxisme.jdbcutil;import Java.io.closeable;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.inputstream;import Java.sql.connection;import Java.sql.DriverManager;import Java.sql.resultset;import java.sql.sqlexception;import Java.sql.statement;import java.util.Properties;/** * JDBC Database Operations Tool class simplifies database operations * @author Wxisme * */public class Jdbcutil {//resource file static Properties Pros = null;//static initialization when loading Jdbcutil class, call static {pros = new Properties (); try {//Load resource file InputStream in = Thread.CurrentThread (). Getcontextclassloader (). getre Sourceasstream ("Db.properties"), if (in = = null) {throw new FileNotFoundException ("config file not found");} Pros.load (in);} catch (IOException e) {e.printstacktrace ();}} /** * Get database connection * @return Connection */public static Connection getmysqlconnection () {Connection conn = null;try {//Load Database driver C Lass.forname (Pros.getproperty ("Mysqldriver"));//Get database Connection conn = Drivermanager.getconnection (Pros.getproperty (" Mysqlurl "), Pros.getproperty (" Mysqluser "), Pros.getproperty ("Mysqlpwd"));} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Return conn;} public static Connection Getoracleconn () {try {class.forname (Pros.getproperty ("Oracledriver")); return Drivermanager.getconnection (Pros.getproperty ("Oracleurl"), Pros.getproperty ("Oracleuser"), Pros.getProperty (" Oraclepwd "));} catch (Exception e) {e.printstacktrace (); return null;}}  /** * Close IO resource * @param io */public static void CloseFile (closeable ... io) {for (closeable Temp:io) {if (temp! = null) {try {Temp.close ();} catch (IOException e) {System.out.println ("file close failed"); E.printstacktrace ();}}} Close the JDBC resource note order public static void Close (ResultSet rs,statement ps,connection conn) {try {if (rs!=null) {rs.close ();}} CATC H (SQLException e) {e.printstacktrace ();} try {if (ps!=null) {ps.close ();}} catch (SQLException e) {e.printstacktrace ();} try {if (conn!=null) {conn.close ();}} catch (SQLException e) {e.printstacktrace ()}} public static void Close (Statement ps,connection conn) {TRy {if (ps!=null) {ps.close ();}} catch (SQLException e) {e.printstacktrace ();} try {if (conn!=null) {conn.close ();}} catch (SQLException e) {e.printstacktrace ()}} public static void Close (Connection conn) {try {if (conn!=null) {Conn.close ()}} catch (SQLException e) {E.printstacktrace ();}}}

Utiltest.java

Package Com.wxisme.jdbcutil;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.ResultSet Import Java.sql.sqlexception;public class Utiltest {public static void main (string[] args) {Connection conn = jdbcutil.ge Tmysqlconnection (); PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from student where id =?"; try {PS = conn.preparestatement (sql);p S.setint (1, 1), rs = Ps.executequery (), while (Rs.next ()) {System.out.println ( Rs.getstring ("name"));}} catch (SQLException e) {e.printstacktrace ();}}}

It can also be further encapsulated as a thread-safe tool class.

Encapsulating the JDBC Tool class

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.