Packageutil; importjava. io. IOException; importjava. SQL. connection; importjava. SQL. driverManager; importjava. SQL. preparedStatement; importjava. SQL. resultSet; importjava. SQL. SQLException; importjava. util. properties; publicclassJdbcUtil {pr
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 {pr
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 ;}}