java jdbc 连接 mysql 数据库, mysql 实现 查询 指定行

来源:互联网 发布:阿里数据分析师面试 编辑:程序博客网 时间:2024/05/18 19:22

         java jdbc 连接数据库是入门级的数据库实验,之所以在这里把代码贴出来 是为了 方便以后阅读。

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 DbStoreHelper _instance = null;private static int count = 0;private static int tot = 0;private static Object lock = new Object();private 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) {try {init();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}count++;psts.setString(1,item.id);psts.setString(2,item.time);psts.setString(3,item.name);psts.setString(4,item.gender);psts.setString(5,item.sign);psts.setDouble(6,item.latitude);psts.setDouble(7,item.longitude);psts.setInt(8,item.dis);psts.addBatch();if (count == every_time) {tot++;System.out.println("commit : " + count * tot);//long start = System.currentTimeMillis();count = 0;commit();//long end = System.currentTimeMillis();//System.out.println("cost time : " + (end - start) / (1000)//+ "s");}}}public void commit() throws SQLException {psts.executeBatch(); //批量执行conn.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";//指定编码方式连接数据库insert_sql = "INSERT INTO "+FinalUtil.new_table_name+" VALUES (?,?,?,?,?,?,?,?)";username = "root";//用户名password = "111111";//密码Class.forName("com.mysql.jdbc.Driver");//加载数据驱动//初始化连接conn = DriverManager.getConnection(connectStr, username, password);conn.setAutoCommit(false); //设置手动提交//获取操作对象psts = conn.prepareStatement(insert_sql);}}



关于mysql 按行查找 数据记录


select * from limit 20 offset 10

意思是 从 第10行开始,查找20行

0 0
原创粉丝点击