The Java JDBC Connection database is an entry-level database experiment where code is posted to facilitate later reading.
Package Com;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.preparedstatement;import Java.sql.sqlexception;public class Dbstorehelper {private string insert_sql;private string Connectstr;private string Username;private String password;private Connection conn = null;private PreparedStatement PSTs = null;private static DbSto Rehelper _instance = null;private static int count = 0;private static int tot = 0;private static Object lock = new Object ( );p rivate final static int every_time = 20000;public static Dbstorehelper get () {if (_instance = = null) {return _instance = new Dbstorehelper ();} return _instance;} Private Dbstorehelper () {}public void Addpacket (Persons item) throws SQLException {synchronized (lock) {if (count = = 0) {T ry {init ();} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} count++;p sts.setstring (1,item.id);p sts.setstring (2,item.time);p sts.setstring (3,item.name);p sts.setstring (4, Item.gender);p sts.setstring (5,item.sign);p sts.setdouble (6,item.latitude);p sts.setdouble (7,item.longitude);p sts.setint (8,item.dis);p sts.addbatch (); if (count = = Every_time ) {tot++; System.out.println ("Commit:" + count * tot);//long start = System.currenttimemillis (); count = 0;commit ();//long end = Sy Stem.currenttimemillis ();//system.out.println ("Cost Time:" + (End-start)/(+)//+ "S");}} public void Commit () throws SQLException {Psts.executebatch ();///Batch execution Conn.commit ();////Bulk commit conn.close ();} private void Init () throws ClassNotFoundException, SQLException {connectstr = "jdbc:mysql://localhost:3306/" + Finalutil.database+ "?useserverprepstmts=false&rewritebatchedstatements=true&useunicode=true& CHARACTERENCODING=UTF8MB4&CHARSET=UTF8MB4 ";//Specify the encoding method to connect to the database Insert_sql =" INSERT into "+finalutil.new_table_name+" VALUES (?,?,?,?,?,?,?,?) "; Username = "root";//user name password = "111111";//Password Class.forName ("Com.mysql.jdbc.Driver");//Load data-driven//Initialize Connection conn = Drivermanager.getconnection (connectstr, username, password); Conn.setautocommit (false); Set ManualSubmit/Get Action Object PSTs = Conn.preparestatement (Insert_sql);}}
About MySQL finding data records by row
SELECT * FROM limit 10 offset
Start with line 10th and look for 20 rows
Java JDBC Connection MySQL database, MySQL implementation query specified row