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

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 exception

e.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行

原文:http://blog.csdn.net/chenwen_201116040110/article/details/42173047

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值