Java JDBC Connection MySQL database, MySQL implementation query specified row

Source: Internet
Author: User

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

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.