Java Dbhelper supports addition, deletion, and modification of big data
Introduction
During project creation, technical selection is very important. The underlying method directly affects the speed of big data access and modification. There are many excellent ORM frameworks in Java, such: JPA, Hibernate, and so on. As we said, the framework has the advantages of the framework, and of course there are some improvements. At this time, we need to address different business needs, different traffic volumes are reencapsulated in the underlying architecture to support addition, deletion, and modification of big data.
Code:
Import java. io. *; import java. SQL. *; import java. util. *; import java. util. logging. level; import java. util. logging. logger; import javax. servlet. jsp. jstl. SQL. *;/*** DbHelper * @ author qmx **/public class Dbhelper {private String SQL; // public void setSql (String SQL) {this. SQL = SQL;} private List sqlValues; // The public void setSqlValues (List sqlValues) {this. sqlValues = sqlValues;} privat E List <list> sqlValue; // The SQL statement parameter public void setSqlValue (List <list> sqlValues) {this. sqlValue = sqlValues;} private Connection con; // Connection object public void setCon (Connection con) {this. con = con;} public Dbhelper () {this. con = getConnection (); // assign the initial value to the Connection object}/*** obtain the database Connection * @ return */private Connection getConnection () {String driver_class = null; string driver_url = null; String database_user = null; String Database_password = null; try {InputStream Fi = this. getClass (). getResourceAsStream ("/db. properties "); // load the database configuration file to the memory Properties p = new Properties (); p. load (FCM); driver_class = p. getProperty ("driver_class"); // obtain the database configuration file driver_url = p. getProperty ("driver_url"); database_user = p. getProperty ("database_user"); database_password = p. getProperty ("database_password"); Class. forName (driver_class); con = DriverManag Er. getConnection (driver_url, database_user, database_password);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} Return con;}/*** shut down the database * @ param con * @ param pst * @ param rst */private void closeAll (Connection con, PreparedStatement pst, ResultSet rst) {if (rst! = Null) {try {rst. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (pst! = Null) {try {pst. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (con! = Null) {try {con. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}/ *** shut down the database * @ param con * @ param pst * @ param rst */private void closeAll (Connection con, Statement pst, ResultSet rst) {if (rst! = Null) {try {rst. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (pst! = Null) {try {pst. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (con! = Null) {try {con. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}/ *** find * @ param SQL * @ param sqlValues * @ return */public Result executeQuery () {Result result = null; resultSet rst = null; PreparedStatement pst = null; try {pst = con. prepareStatement (SQL); if (sqlValues! = Null & sqlValues. size ()> 0) {// setSqlValues (pst, sqlValues) when a placeholder exists in the SQL statement;} rst1_pst.exe cuteQuery (); result = ResultSupport. toResult (rst); // be sure to complete the conversion before closing the database} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {this. closeAll (con, pst, rst);} return result;}/*** add, delete, and modify * @ return */public int executeUpdate () {int result =-1; preparedStatement pst = null; try {pst = con. prepare Statement (SQL); if (sqlValues! = Null & sqlValues. size ()> 0) {// setsqlvalues(pst,sqlvalues{{result}pst.exe cuteUpdate ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {this. closeAll (con, pst, null);} return result;}/*** use PreparedStatement to add batch methods * @ return */public int [] executeUpdateMore () {int [] result = null; try {PreparedStatement prest = con. prepareStatement (SQL, ResultSet. T YPE_SCROLL_SENSITIVE, ResultSet. CONCUR_READ_ONLY); for (List sqlValueString: sqlValue) {for (int I = 0; I <sqlvaluestring. size (); I ++) {try = "" catch = "" sqlexception = "" todo = "" auto-generated = "" block = "" return = "" strvalue: = "" insert = "" throws = "" public = "" result = "null;" statement = "" stmt = "con. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, "sqlstring =" null; "string =" "strvalue =" ": =" "boolean =" "boolean Autocommit = "false; "exception =" "param =" "pst =" "sqlvalues =" "private =" "void =" "preparedstatement =" "list =" "int =" "I =" 0; I <sqlValues. size (); I ++) {"pre =" "> in db. properties: <p> </p> <pre class = "brush: java; "> driver_class = com. mysql. jdbc. driverdriver_url = jdbc: mysql: // 192.168.22.246: 3306/importexceltestdatabase_user = basicdatabase_password = basic </pre> <p> </p> <p> test: </p> </ P> <pre class = "brush: java;"> import java. util. *; public class ImportExcelTest {public static void main (String [] args) {/* Dbhelper db = new Dbhelper (); String SQL = "insert into tb_coursetype (id, courseTypeName) values ('200', 'qmx3 ') "mongodb.setsql(sqlmongodb.exe cuteUpdate (); * // * Dbhelper db1 = new Dbhelper (); String sql1 =" insert into tb_coursetype (id, courseTypeName) values (?,?) "; List sqlValues = new ArrayList (); sqlValues. add ("2012004"); sqlValues. add ("qmx4" );db1.setSqlValues(sqlValues);db1.setSql(sql1);db1.exe cuteUpdate (); */Dbhelper db = new Dbhelper (); String SQL = "insert into tb_coursetype (id, courseTypeName) values (?,?) "; List <list> sqlValues = new ArrayList (); List sqlValueString = new ArrayList (); sqlValueString. add ("2012010"); sqlValueString. add ("qmx10"); sqlValues. add (sqlValueString); List sqlValueString1 = new ArrayList (); sqlValueString1.add ("2012011"); sqlValueString1.add ("qmx11"); sqlValueString1.add. add (sqlValueString1); List sqlValueString2 = new ArrayList (); sqlValueString2.add ("2012012"); sqlValueString2.add ("qmx12"); sqlValues. add (sqlValueString2); List sqlValueString3 = new ArrayList (); sqlValueString3.add ("2012013"); sqlValueString3.add ("qmx13" );sqlValues.add(sqlValueString3);db.setSqlValue(sqlValues);db.setSql( SQL );db.exe cuteUpdateMore ();}} </list> </pre> </p> </sqlvaluestring. size (); I ++) {> </list>