Java implementation of the JdbcTemplate tool

Source: Internet
Author: User

1.DButil

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;import java.util.resourcebundle;/** * Database Operations Tool class * @author Xrhou * */public class Dbutils {//database connection Address public static string url;//user name public static string username;//password public static string Password;//mysql driver class public static String driver;private static ResourceBundle RB = Resourcebundle.getbundle ("Com.xrhou.util.db.db-config");p rivate Dbutils () {}//uses a static block to load the driver Static{url = rb.getstring ("Jdbc.url"); USERNAME = rb.getstring ("Jdbc.username"); PASSWORD = rb.getstring ("Jdbc.password");D river = rb.getstring ("Jdbc.driver"); try {class.forname (DRIVER);} catch ( ClassNotFoundException e) {e.printstacktrace ();}} Defines a method to get a database connection public static Connection getconnection () {Connection conn = null;try {conn = Drivermanager.getconnection (URL, USERNAME, PASSWORD);} catch (SQLException e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Get Connection Failed");} Return conn;} /** * Close Database connection * @param RS * @parAM STAT * @param conn */public static void Close (ResultSet rs,statement stat,connection conn) {try {if (rs!=null) rs.close (); if (stat!=null) stat.close (); if (conn!=null) Conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}

2.JdbcTemplete

Import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;public class Jdbctemplete {/** * to implement additions and deletions of the abstract * @param SQL * @param args */public int update (String sql, Object ... args) {Connection conn = null; PreparedStatement PS = Null;try{conn = Dbutils.getconnection ();p s = conn.preparestatement (sql);//Set the placeholder parameter if (args!= NULL) {for (int i = 0; i < args.length; i++) {Ps.setobject (i+1, args[i]);}} return Ps.executeupdate ();} catch (SQLException e) {e.printstacktrace (); return-1;} Finally{dbutils.close (NULL, PS, conn);}} /** * Abstract of Query method * @param SQL * @param handler * @param args * @return */public Object query (String sql,resultsethandler hand Ler,object. args) {Connection conn = null; PreparedStatement PS = null; ResultSet rs = null;try {conn = Dbutils.getconnection ();p s = conn.preparestatement (sql), if (Args!=null) {for (int i = 0; I & Lt Args.length; i++) {ps.setobject (i+1, args[i]);}} rs = Ps.executequery (); return Handler.dohandler (RS);} catch (SqlexCeption e) {e.printstacktrace (); return null;}}} 

3.ResultSetHandler processing

Import Java.sql.resultset;import Java.sql.sqlexception;public interface Resultsethandler {public Object Dohandler ( ResultSet rs) throws SQLException;}

4. Properties File

jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/friendsjdbc.username=rootjdbc.password= Root

  

Java implementation of the JdbcTemplate tool

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.