⑴ java怎麼多線程操作一個資料庫
//將資料庫中的數據條數分段
public void division(){
//獲取要導入的總的數據條數
String sql3="SELECT count(*) FROM [CMD].[dbo].[my1]";
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;
resie=sum%30000;
}else{
resie=sum;
}
System.out.println(n+" "+resie);
} 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].[my1]) 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.my2 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());
}
}
⑵ java中如何用多線程訪問資料庫
//將資料庫中的數據條數分段 public void division(){ //獲取要導入的總的數據條數 String sql3="SELECT count(*) FROM [CMD].[dbo].[my1]"; 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; resie=sum%30000; }else{ resie=sum; } System.out.println(n+" "+resie); } 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].[my1]) 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.my2 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()); } }
⑶ java爬蟲採用多線程,資料庫連接多了就報異常
可以通過測試對錯誤原因進行排查,如果連接少量時不出問題,那就是連接量超出了某一數值出現異常,可以查看資料庫的鏈接上限,如果是連接上限原因,可以嘗試上調上限或者降低連接數量
⑷ java 多線程存儲資料庫
以mysql為資料庫寫的一個粗陋的demo,你參考一下,希望不會因為代碼過多被網路吞了——
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
allotThread();
}
/**
* 將100條數據分成10份並啟動10個線程分別操作
*/
public static void allotThread() {
List<String[]> datas = buildDatas();
for (int i=0; i<100; i+=10) {
List<String[]> tenDatas = datas.subList(i, i + 10);
insertData(tenDatas);
}
}
/**
* 創建100條模擬數據
* @return
*/
public static List<String[]> buildDatas() {
List<String[]> datas = new ArrayList<String[]>();
for (int i=0; i<100; i++) {
String[] data = {"id " + i, "name " + i};
datas.add(data);
}
return datas;
}
/**
* 啟動線程進行數據插入操作
* @param tenDatas
*/
public static void insertData(final List<String[]> tenDatas) {
new Thread(new Runnable() {
public void run() {
String sql = "insert into testtable (id, name) values (?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
pstmt = getPstmt(conn, sql);
for (String[] data : tenDatas) {
pstmt.setString(1, data[0]);
pstmt.setString(2, data[1]);
pstmt.addBatch();
}
pstmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
rollback(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
close(pstmt);
close(conn);
}
}
}).start();
}
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";
Connection conn = DriverManager.getConnection(dbUrl, "root", "tooeasy");
return conn;
}
public static PreparedStatement getPstmt(Connection conn, String sql) throws SQLException, ClassNotFoundException {
PreparedStatement pstmt = conn.prepareStatement(sql);
return pstmt;
}
public static void rollback(Connection conn) {
try {
if (null != conn) {
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn) {
try {
if (null != conn) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(PreparedStatement pstmt) {
try {
if (null != pstmt) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if (null != rs) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
⑸ Java多線程並發操作資料庫能否提高運行速度。
優化建議:
1. 在你的代碼里大點評測下 每個操作花的時間. 譬如 dom4j解析花了多久, 存儲到資料庫花了多久等等.
2. 評測哪些地方可以並行操作以提高CPU利用率;
3. 資料庫操作部分也可以做適當優化, 譬如批量提交可以顯著提高插入速度, 譬如去除索引/主鍵後插入等;
4. 不同機器的IO速度是不同的, 因此應該能提供運行時的任務調度參數化, 譬如多少個dom4j解析線程, 入庫的批量數量等;
⑹ java 多線程操作資料庫
//將資料庫中的數據條數分段
publicvoiddivision(){
//獲取要導入的總的數據條數
Stringsql3="SELECTcount(*)FROM[CMD].[dbo].[my1]";
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;
resie=sum%30000;
}else{
resie=sum;
}
System.out.println(n+""+resie);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
線程類
publicMyThread(intstart,intend){
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(Exceptione){
e.printStackTrace();
}
//TODOAuto-generatedconstructorstub
}
publicArrayList<Member>getAll(){
Membermember;
Stringsql1="select*from(selectrow_number()over(orderbypmcode)asrowNum,*"+
"from[CMD].[dbo].[my1])astwhererowNumbetween"+start+"and"+end;
try{
System.out.println("正在獲取數據...");
allmembers=newArrayList();
rss=stas.executeQuery(sql1);
while(rss.next()){
member=newMember();
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資料庫數據!");
returnallmembers;
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
System.out.println("獲取sqlserver資料庫數據發送異常!");
e.printStackTrace();
}
try{
rss.close();
stas.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnnull;
}
publicvoidinputAll(ArrayList<Member>allmembers){
System.out.println("開始向mysql中寫入");
Stringsql2="insertintotest.my2values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try{
ps=con.prepareStatement(sql2);
System.out.println("-------------------------等待寫入數據條數:"+allmembers.size());
for(inti=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(SQLExceptione){
//TODOAuto-generatedcatchblock
System.out.println("向mysql中更新數據時發生異常!");
e.printStackTrace();
}
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true&&flag){
this.inputAll(getAll());
}
}
⑺ java多線程連接資料庫問題
是的。肯定要每次都連接資料庫,簡單點
你可以把連接資料庫寫成一個類中的方法,到run()線程的時候,創建類的對象,再調用就好了。
⑻ java多線程操作資料庫
首先,你的確定數據的主鍵是否可以使用序列自增。
如果可以那麼你可以在插入數據的時候獲取序列里的值,這樣數據就不會重復了。
其次,可以創建一個公共方法,進行數據的插入操作,並且方法類型聲明為 static synchronized
類型,這樣基本上就不會出現數據重復的現象了
最後,要看你是怎麼獲得待插入源數據了,這個獲得數據源的方法也做成static synchronized的公
共方法。