如何實作呼叫ACCESS查詢出的資料并插入SQLSERVER
import java.sql.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
public class AcessL extends JFrame{
private static final int DEFAULT_WEIDTH = 1200;
private static final int DEFAULT_HEIGHT = 780;
private JScrollPane scpDemo;
private JTable tabDemo;
private Connection conn1;
private Connection conn;
private String connStr, sqlStr;
private String useName, passWord;
private PreparedStatement ps;
private StringBuffer strBuf = new StringBuffer();
public AcessL()
{
JFrame f=new JFrame();
f.setTitle("資料提取器");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(DEFAULT_WEIDTH, DEFAULT_HEIGHT);
f.setVisible(true);
f.setResizable(false);
f.setLayout(null);
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(40,110,1100,500);
JButton button =new JButton("提交");//實體化button并添加標題
f.add(button); //添加button 到視窗中
button.setFont(new Font("黑體",1,30));
button.setBounds(350, 30, 500, 40);
try {
btnShow();
} catch (InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 將組件加入到表單中
f.add(this.scpDemo);
}
public void btnShow() throws InstantiationException, IllegalAccessException{
String sql = "select * from student";
strBuf.delete(0, strBuf.length());
sqlStr = "insert into student(ID, name1, name2) values(?, ?, ?)";
try{
// 獲得連接
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
System.out.println("驅動加載成功!");
Connection conn1 = DriverManager.getConnection("jdbc:Access:///D:/1111/Database1.mdb","","");
System.out.println("資料庫連接成功!");
PreparedStatement pstm = conn1.prepareStatement(sql);
ResultSet rs = pstm.executeQuery(sql);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 資料庫驅動加載
System.out.println("驅動加載成功!");
connStr = "jdbc:sqlserver://10.80.10.120:1433;DatabaseName=fhadmin"; // 定義連接資料庫URL
useName = "sa";
passWord = "nmcy123.";
conn = DriverManager.getConnection(connStr, useName, passWord); // 連接資料庫
System.out.println("資料庫連接成功!");
// 計算有多少條記錄
int count = 0;
while(rs.next()){
count++;
}
while(rs.next()){
try
{
ps = conn.prepareStatement(sqlStr);
ps.setString(1, rs.getString("ID"));
ps.setString(2, "4");
ps.setString(3, "5");
ps.executeUpdate();
} catch (Exception e)
{
e.printStackTrace();
}
}
rs = pstm.executeQuery();
// 將查詢獲得的記錄資料,轉換成適合生成JTable的資料形式
Object[][] info = new Object[count][3];
count = 0;
while(rs.next()){
info[count][0] = rs.getString("ID");
info[count][1] = rs.getString("name");
info[count][2] = rs.getString("name1");
count++;
}
// 定義表頭
String[] title = {"化驗單號","化驗元素1","化驗元素2"};
// 創建JTable
this.tabDemo = new JTable(info,title);
// 顯示表頭
//this.jth = this.tabDemo.getTableHeader();
// 將JTable加入到帶滾動條的面板中
this.scpDemo.getViewport().add(tabDemo);
rs.close();
conn1.close();
conn.close();
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"資料源錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"資料操作錯誤","錯誤",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new AcessL();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/268585.html
標籤:Java EE
上一篇:idea里tomcat啟動后,無法打開Geoserver
下一篇:java驗證碼
