Java (version 2014) tool for connecting to the database

Source: Internet
Author: User

Package util; import java. io. IOException; 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 {private static Connection conn; private static PreparedStatement pstmt; private static ResultSet rs; private JdbcUtil () {super (); // TODO Auto-generated constructor stu B} // public static Connection getConn () {if (conn = null) {// load the property configuration file // create the property object Properties prop = new Properties (); try {// load the property file prop with the specified name. load (JdbcUtil. class. getClassLoader (). getResourceAsStream ("jdbc. properties "); try {// find the Class file Class Based on the file name. forName (prop. getProperty ("driver"); // create a connection object try {conn = DriverManager. getConnection (prop. getProperty ("url"), prop. getProperty ("user"), prop. getProperty ("Pass"); System. out. println ("successfully connected to the database");} catch (SQLException e) {// TODO Auto-generated catch blockSystem. out. println ("failed to connect to the Database"); e. printStackTrace () ;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem. out. println ("no" + prop. getProperty ("driver") + "file"); e. printStackTrace () ;}} catch (IOException e) {// TODO Auto-generated catch blockSystem. out. println ("attribute file loading error"); e. pri NtStackTrace () ;}} return conn;} public static void release (ResultSet rs, PreparedStatement pstmt) {// release the result set if (rs! = Null) {try {rs. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}// release preparation statement if (pstmt! = Null) {try {pstmt. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}// add, delete, and modify the encapsulation method public static boolean upDate (String SQL, Object [] obj) {boolean flag = false; try {// creation of the prepared statement, object with SQL command pstmt = getConn (). prepareStatement (SQL); for (int I = 1; I <= obj. length; I ++) {pstmt. setObject (I, obj [I-1]);} int I = pstmt.exe cuteUpdate (); if (I> 0) {flag = true ;} Catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}finally {release (rs, pstmt) ;}return flag ;}// batch Delete public static boolean updateBatchDel (String SQL, Object [] ids) {boolean flag = false; Connection conn = getConn (); PreparedStatement pstmt = null; ResultSet rs = null; try {conn. setAutoCommit (false); pstmt = conn. prepareStatement (SQL); for (int I = 0; I <ids. length; I ++) {Pstmt. setObject (1, ids [I]); System. out. println (SQL + "---------------" + ids [I]); pstmt. addBatch () ;}int [] num = pstmt.exe cuteBatch (); // run for (int I = 0; I <num. length; I ++) {if (num [I] = 0) {try {conn. rollback (); // perform transaction rollback return flag;} catch (SQLException ex) {ex. printStackTrace () ;}} conn. commit (); // submit the transaction flag = true;} catch (SQLException e) {e. printStackTrace ();} finally {release (rs, pstm T);} return flag;} // obtain all the pages of the input table based on the name of the input table and the data on each page. // tableName ::::: name of the data table for the Operation // pagesize :::::: number of information entries displayed on each page public static Integer getCountPage (String tableName, Integer pagesize) {Integer countPage = 0; string SQL = "select count (*) as c from" + tableName; Connection conn = JdbcUtil. getConn (); PreparedStatement pstmt = null; ResultSet rs = null; conn = JdbcUtil. getConn (); try {pstmt = conn. prepareStatement (SQL); r S = pstmt.exe cuteQuery (); if (rs. next () {int countRecord = rs. getInt ("c"); countPage = countRecord % pagesize = 0? CountRecord/pagesize: countRecord/pagesize + 1 ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {JdbcUtil. release (rs, pstmt);} return countPage ;}}

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.