java 多线程操作数据库
这个是DBConn类单人访问没有问题,但是多人一起操作数据库就会有问题,由于是同一个stmt在每次createConn的时候都会先关闭stmt,之前我都不知道,直到我做了...
这个是DBConn类 单人访问没有问题,但是多人一起操作数据库就会有问题,由于是同一个stmt 在每次createConn的时候 都会先关闭stmt,之前我都不知道,直到我做了个导入大文件的时候才发现这么写同时操作数据库是不行的,但是我又不知道怎么改,外部调用是DBConn点方法名
private static Connection conn;
private static Statement stmt;
public static Connection getConn() {
return conn;
}
public static boolean createConn() {
closedb();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print", "root", "root");
stmt = conn.createStatement();
return true;
} catch (Exception e) {
return false;
}
}
public static ResultSet executeQuerySql(String sql) throws SQLException {
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
public static void closedb() {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println(e);
} 展开
private static Connection conn;
private static Statement stmt;
public static Connection getConn() {
return conn;
}
public static boolean createConn() {
closedb();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print", "root", "root");
stmt = conn.createStatement();
return true;
} catch (Exception e) {
return false;
}
}
public static ResultSet executeQuerySql(String sql) throws SQLException {
ResultSet rs = stmt.executeQuery(sql);
return rs;
}
public static void closedb() {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println(e);
} 展开
4个回答
展开全部
//将数据库中的数据条数分段
public void division(){
//获取要导入的总的数据条数
String sql3="SELECT count(*) FROM [CMD].[dbo].[mycopy1]";
try {
pss=cons.prepareStatement(sql3);
rss=pss.executeQuery();
while(rss.next()){
System.out.println("总记录条数:"+rss.getInt(1));
sum=rss.getInt(1);
}
//每30000条记录作为一个分割点
if(sum>=30000){
n=sum/30000;
residue=sum%30000;
}else{
residue=sum;
}
System.out.println(n+" "+residue);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
线程类
public MyThread(int start,int end) {
this.end=end;
this.start=start;
System.out.println("处理掉余数");
try {
System.out.println("--------"+Thread.currentThread().getName()+"------------");
Class.forName(SQLSERVERDRIVER);
System.out.println("加载sqlserver驱动...");
cons = DriverManager.getConnection(CONTENTS,UNS,UPS);
stas = cons.createStatement();
System.out.println("连接SQLServer数据库成功!!");
System.out.println("加载mysql驱动.....");
Class.forName(MYSQLDRIVER);
con = DriverManager.getConnection(CONTENT,UN,UP);
sta = con.createStatement();
// 关闭事务自动提交
con.setAutoCommit(false);
System.out.println("连接mysql数据库成功!!");
} catch (Exception e) {
e.printStackTrace();
}
// TODO Auto-generated constructor stub
}
public ArrayList<Member> getAll(){
Member member;
String sql1="select * from (select row_number() over (order by pmcode) as rowNum,*" +
" from [CMD].[dbo].[mycopy1]) as t where rowNum between "+start+" and "+end;
try {
System.out.println("正在获取数据...");
allmembers=new ArrayList();
rss=stas.executeQuery(sql1);
while(rss.next()){
member=new Member();
member.setAddress1(rss.getString("address1"));
member.setBnpoints(rss.getString("bnpoints"));
member.setDbno(rss.getString("dbno"));
member.setExpiry(rss.getString("expiry"));
member.setHispoints(rss.getString("hispoints"));
member.setKypoints(rss.getString("kypoints"));
member.setLevels(rss.getString("levels"));
member.setNames(rss.getString("names"));
member.setPmcode(rss.getString("pmcode"));
member.setRemark(rss.getString("remark"));
member.setSex(rss.getString("sex"));
member.setTelephone(rss.getString("telephone"));
member.setWxno(rss.getString("wxno"));
member.setPmdate(rss.getString("pmdate"));
allmembers.add(member);
// System.out.println(member.getNames());
}
System.out.println("成功获取sqlserver数据库数据!");
return allmembers;
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("获取sqlserver数据库数据发送异常!");
e.printStackTrace();
}
try {
rss.close();
stas.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void inputAll(ArrayList<Member> allmembers){
System.out.println("开始向mysql中写入");
String sql2="insert into test.mycopy2 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
ps=con.prepareStatement(sql2);
System.out.println("-------------------------等待写入数据条数: "+allmembers.size());
for(int i=0;i<allmembers.size();i++){
ps.setString(1, allmembers.get(i).getPmcode());
ps.setString(2, allmembers.get(i).getNames());
//System.out.println(allmembers.get(i).getNames());
ps.setString(3, allmembers.get(i).getSex());
ps.setString(4, allmembers.get(i).getTelephone());
ps.setString(5, allmembers.get(i).getAddress1());
ps.setString(6, allmembers.get(i).getPmdate());
ps.setString(7, allmembers.get(i).getExpiry());
ps.setString(8, allmembers.get(i).getLevels());
ps.setString(9, allmembers.get(i).getDbno());
ps.setString(10, allmembers.get(i).getHispoints());
ps.setString(11, allmembers.get(i).getBnpoints());
ps.setString(12, allmembers.get(i).getKypoints());
ps.setString(13, allmembers.get(i).getWxno());
ps.setString(14, allmembers.get(i).getRemark());
//插入命令列表
//ps.addBatch();
ps.executeUpdate();
}
//ps.executeBatch();
con.commit();
ps.close();
con.close();
this.flag=false;
System.out.println(Thread.currentThread().getName()+"--->OK");
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("向mysql中更新数据时发生异常!");
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true&&flag){
this.inputAll(getAll());
}
}
展开全部
楼上说的对,
你这个类写的不健壮,
多访问,应该写个连接池的. 给你写点核心代码,参考一下
private static final vector pool=new vector();
private static final int MAX_SIZE=10;
private static final int MIN_SIZE=3;
private static Connection createConnection(){
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection( ("jdbc:mysql://localhost:3306/print", "root", "root");
}
static {
for(int i=0;i<MIN_SIZE;i++){
pool.add(createConnection());
}
}
public static synchronized Connection getConnection() {
Connection conn = null;
if (pool.isEmpty()) {
conn = createConnection();
} else {
int last_idx = pool.size() - 1;
conn = (Connection) pool.get(last_idx);
pool.remove(conn);
}
return conn;
}
public static synchronized void close(Connection conn){
if(pool.size()<MAX_SIZE){
pool.add(conn);
}else{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
你这个类写的不健壮,
多访问,应该写个连接池的. 给你写点核心代码,参考一下
private static final vector pool=new vector();
private static final int MAX_SIZE=10;
private static final int MIN_SIZE=3;
private static Connection createConnection(){
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection( ("jdbc:mysql://localhost:3306/print", "root", "root");
}
static {
for(int i=0;i<MIN_SIZE;i++){
pool.add(createConnection());
}
}
public static synchronized Connection getConnection() {
Connection conn = null;
if (pool.isEmpty()) {
conn = createConnection();
} else {
int last_idx = pool.size() - 1;
conn = (Connection) pool.get(last_idx);
pool.remove(conn);
}
return conn;
}
public static synchronized void close(Connection conn){
if(pool.size()<MAX_SIZE){
pool.add(conn);
}else{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1. 不要每次访问,都重新连接
2. 这里不是stmt被关闭了,而是你新建对象的时候把原来stmt的引用丢弃了。不要多个访问公用一组变量。
2. 这里不是stmt被关闭了,而是你新建对象的时候把原来stmt的引用丢弃了。不要多个访问公用一组变量。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已进行基本修改,有必要使用Dao和DataSource,使用连接池技术进行优化。
代码几乎未作变动。
private static Connection conn;
private static Statement stmt;
public static Connection getConn() {
return conn;
}
public static boolean createConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print", "root", "root");
stmt = conn.createStatement();
return true;
} catch (Exception e) {
return false;
}
}
public static ResultSet executeQuerySql(String sql) throws SQLException {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
return rs;
} finally{
closedb();
}
}
public static void closedb() {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
代码几乎未作变动。
private static Connection conn;
private static Statement stmt;
public static Connection getConn() {
return conn;
}
public static boolean createConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/print", "root", "root");
stmt = conn.createStatement();
return true;
} catch (Exception e) {
return false;
}
}
public static ResultSet executeQuerySql(String sql) throws SQLException {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
return rs;
} finally{
closedb();
}
}
public static void closedb() {
try {
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |