JAVA与PostgreSQL的导入模块
***jxl模?在?网站下?
package com.gmxsd.ImportData;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.io.*;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.Cell;
import java.util.*;
import jxl.Sheet;
public class ImportData extends JDialog {
JTextField ExcelFile;//Excel文件名
JComboBox ExcelField;//Excel工作表名
JComboBox ExcelCol; //Excel列名
JTextField serverPath;//服?器名
JTextField database;//?据?名
JTextField user;//用?名
JPasswordField password;//密?
JComboBox tables;//?据??料表名
JComboBox tableFields;//?料表中的列名
DefaultTableModel dtm ;
JTable jtable;
Connection cn=null;
Statement st =null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
JFileChooser filechooser=null;
File file = null;
Workbook wk;
String excel;
String seltable;
public ImportData(){
this.setTitle("?据?入模?");
this.setSize(550,400);
buildFrame();
this.setResizable(false);
this.setVisible(true);
}
protected void buildFrame(){
JPanel contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
JPanel centerJPanel = buildCenterJPanel();
centerJPanel.setBounds(new Rectangle(0,0,540,150));
JPanel southJPanel = buildSouthJPanel();
southJPanel.setBounds(new Rectangle(0,150,540,210));
contentPane.add(centerJPanel,null);
contentPane.add(southJPanel,null);
}
protected JPanel buildCenterJPanel(){
JPanel cpanel = new JPanel();
cpanel.setLayout(new GridLayout());
JPanel epanel = buildEPanel();
epanel.setBorder(BorderFactory.createTitledBorder("?据源"));
JPanel wpanel = buildWPanel();
wpanel.setBorder(BorderFactory.createTitledBorder("目?源"));
cpanel.add(epanel);
cpanel.add(wpanel);
return cpanel;
}
protected JPanel buildEPanel(){
JPanel sourcePanel = new JPanel();
sourcePanel.setLayout(null);
JLabel jlabel1 = new JLabel("Excel文件");
jlabel1.setBounds(new Rectangle(10,15,60,20));
sourcePanel.add(jlabel1,null);
ExcelFile = new JTextField();
ExcelFile.setBounds(new Rectangle(65,15,140,20));
sourcePanel.add(ExcelFile,null);
JButton selfile = new JButton("??");
selfile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){FileBrowser();}});
selfile.setBounds(new Rectangle(205,15,60,20));
sourcePanel.add(selfile,null);
JLabel jlabel2 = new JLabel("表 ?");
jlabel2.setBounds(new Rectangle(10,40,60,20));
sourcePanel.add(jlabel2,null);
ExcelField = new JComboBox();
ExcelField.setBounds(new Rectangle(65,40,140,20));
sourcePanel.add(ExcelField,null);
JButton openbtn = new JButton("打?");
openbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){FileOpen();}});
openbtn.setBounds(new Rectangle(205,40,60,20));
sourcePanel.add(openbtn,null);
return sourcePanel;
}
protected JPanel buildWPanel(){
JPanel dePanel = new JPanel();
dePanel.setLayout(null);
JLabel jlabel1 = new JLabel("服?器:");
jlabel1.setBounds(new Rectangle(10,15,60,20));
dePanel.add(jlabel1,null);
serverPath = new JTextField("192.168.3.6:5432");
serverPath.setBounds(new Rectangle(65,15,200,20));
dePanel.add(serverPath,null);
JLabel jlabel2 = new JLabel("?据?:");
jlabel2.setBounds(new Rectangle(10,40,60,20));
dePanel.add(jlabel2,null);
database = new JTextField("gaomingdb9");
database.setBounds(new Rectangle(65,40,200,20));
dePanel.add(database,null);
JLabel jlabel3 = new JLabel("用?名:");
jlabel3.setBounds(new Rectangle(10,65,60,20));
dePanel.add(jlabel3,null);
user = new JTextField("postgres");
user.setBounds(new Rectangle(65,65,200,20));
dePanel.add(user,null);
JLabel jlabel4 = new JLabel("密 ?");
jlabel4.setBounds(new Rectangle(10,90,60,20));
dePanel.add(jlabel4,null);
password = new JPasswordField("gaoming");
password.setBounds(new Rectangle(65,90,140,20));
dePanel.add(password,null);
JButton btn = new JButton("?接");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){connectDatabase();}});
btn.setBounds(new Rectangle(205,90,60,20));
dePanel.add(btn,null);
JLabel jlabel5 = new JLabel("?据表名");
jlabel5.setBounds(new Rectangle(10,115,60,20));
dePanel.add(jlabel5,null);
tables = new JComboBox();
tables.setBounds(new Rectangle(65,115,140,20));
dePanel.add(tables,null);
JButton seebtn = new JButton("?示");
seebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){viewTableFields();}});
seebtn.setBounds(new Rectangle(205,115,60,20));
dePanel.add(seebtn,null);
return dePanel;
}
protected JPanel buildSouthJPanel(){
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(BorderFactory.createEtchedBorder());
JPanel inner1 = new JPanel();
JPanel inner2 = new JPanel();
JPanel inner3 = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(inner1,BorderLayout.NORTH);
bottomPanel.add(inner2,BorderLayout.CENTER);
bottomPanel.add(inner3,BorderLayout.SOUTH);
inner1.setLayout(new GridLayout(1,6));
JLabel jlabel7 = new JLabel("??操作",JLabel.CENTER);
inner1.add(jlabel7);
ExcelCol = new JComboBox();
inner1.add(ExcelCol);
JLabel jlabel8 = new JLabel("<---->",JLabel.CENTER);
inner1.add(jlabel8);
tableFields = new JComboBox();
inner1.add(tableFields);
JButton jlbtn = new JButton("建立??");
jlbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){buildConnect();}});
inner1.add(jlbtn);
JButton delbtn = new JButton("?除??");
delbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){delConnect();}});
inner1.add(delbtn);
inner2.setLayout(new GridLayout());
String colnames[]={"Excel列名","?料表列名"};
Object rowdata[][]=null;
dtm = new DefaultTableModel(rowdata,colnames);
jtable = new JTable(dtm);
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(jtable);
inner2.add(jsp);
inner3.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton surebtn = new JButton("?入?据");
surebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){DataImport();}});
JButton cancle = new JButton("返 回");
cancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){DLGHide();}});
inner3.add(surebtn);
inner3.add(cancle);
return bottomPanel;
}
//Excel文件??
protected void FileBrowser(){
filechooser = new JFileChooser("C://");
int result = filechooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
ExcelFile.setText(file.getPath().toString());
try{
wk = Workbook.getWorkbook(file);
String sheets[] = wk.getSheetNames();
ExcelField.removeAllItems();
for(int i=0;i<sheets.length;i++){
ExcelField.addItem(sheets[i]);
}
}
catch(BiffException e){}
catch(IOException e){}
}
}
//Excel文件打?
protected void FileOpen(){
ExcelCol.removeAllItems();
excel= ExcelField.getSelectedItem().toString();
Cell cd[]=wk.getSheet(excel).getRow(0);
for(int i=0;i<cd.length;i++){
ExcelCol.addItem(cd[i].getContents());
}
}
protected void connection(){
String spath = serverPath.getText().trim();
String db = database.getText().trim();
String use = user.getText().trim();
final char str[] = password.getPassword();
final String pwd = new String(str);
String databasepath = "jdbc:postgresql://"+spath+"/"+db;
try{
Class.forName("org.postgresql.Driver");
cn = DriverManager.getConnection(databasepath,use,pwd);
st = cn.createStatement();
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(this,"?据?????失??");
this.dispose();
}
catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(this,"?据??接失?!");
return ;}
}
//?料??接
protected void connectDatabase(){
try{
connection();
String sql = "select * from pg_stat_user_tables";
rs = st.executeQuery(sql);
while(rs.next()){
tables.addItem(rs.getString("relname"));
}
rs.close();
st.close();
cn.close();
}
catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(this,"?据??行失?!");
return ;}
}
//?示?料表中的有效列名
protected void viewTableFields(){
connection();
seltable = tables.getSelectedItem().toString();
String sql = " select * from "+ seltable;
try{
tableFields.removeAllItems();
rs = st.executeQuery(sql);
rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
for(int i=1;i<=colnum;i++){
tableFields.addItem(rsmd.getColumnName(i));
}
rs.close();
st.close();
cn.close();
}
catch(SQLException e){}
}
//建立??
protected void buildConnect(){
String sdata = ExcelCol.getSelectedItem().toString();
String ddata = tableFields.getSelectedItem().toString();
Object rowdata[]={sdata,ddata};
dtm.addRow(rowdata);
}
//?除??
protected void delConnect(){
int nrow = jtable.getSelectedRow();
if(nrow<0){
JOptionPane.showMessageDialog(this,"??需?除的??!");
return ;
}
dtm.removeRow(nrow);
}
//?据?料的?入
protected void DataImport(){
Sheet selsheet = wk.getSheet(excel);
int num = jtable.getRowCount();
String[] totalcount = new String[num];
String[] tf = new String[num];
connection();
try{
// cn.setAutoCommit(false);
for (int i = 1; i < selsheet.getRows(); i++) {
int j = 0;
for (j = 0; j < jtable.getRowCount(); j++) {
String ecol = jtable.getValueAt(j, 0).toString();
int d = selsheet.findCell(ecol).getColumn(); //Excel中的列
totalcount[j] = selsheet.getCell(d, i).getContents();
tf[j] = jtable.getValueAt(j, 1).toString();
}
String sql = "INSERT INTO " + seltable + "(";
String value = ") VALUES(";
for (int k = 0; k < j; k++) {
sql = sql + tf[k] + ",";
value = value + totalcount[k] + ",";
}
try {
int d = sql.lastIndexOf(",");
sql = sql.substring(0, d);
d = value.lastIndexOf(",");
value = value.substring(0, d);
String str = sql + value + ")";
System.out.println(str);
st.executeUpdate(str);
}
catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "?入失?");
//cn.rollback();
return;
}
}
rs.close();
st.close();
cn.close();
JOptionPane.showMessageDialog(this, "?入成功");
}
catch(SQLException e){e.printStackTrace();}
}
protected void DLGHide(){
System.exit(0);
}
public static void main(String args[]){
new ImportData();
}
}
***jxl模?在?网站下?
package com.gmxsd.ImportData;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.io.*;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.Cell;
import java.util.*;
import jxl.Sheet;
public class ImportData extends JDialog {
JTextField ExcelFile;//Excel文件名
JComboBox ExcelField;//Excel工作表名
JComboBox ExcelCol; //Excel列名
JTextField serverPath;//服?器名
JTextField database;//?据?名
JTextField user;//用?名
JPasswordField password;//密?
JComboBox tables;//?据??料表名
JComboBox tableFields;//?料表中的列名
DefaultTableModel dtm ;
JTable jtable;
Connection cn=null;
Statement st =null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
JFileChooser filechooser=null;
File file = null;
Workbook wk;
String excel;
String seltable;
public ImportData(){
this.setTitle("?据?入模?");
this.setSize(550,400);
buildFrame();
this.setResizable(false);
this.setVisible(true);
}
protected void buildFrame(){
JPanel contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
JPanel centerJPanel = buildCenterJPanel();
centerJPanel.setBounds(new Rectangle(0,0,540,150));
JPanel southJPanel = buildSouthJPanel();
southJPanel.setBounds(new Rectangle(0,150,540,210));
contentPane.add(centerJPanel,null);
contentPane.add(southJPanel,null);
}
protected JPanel buildCenterJPanel(){
JPanel cpanel = new JPanel();
cpanel.setLayout(new GridLayout());
JPanel epanel = buildEPanel();
epanel.setBorder(BorderFactory.createTitledBorder("?据源"));
JPanel wpanel = buildWPanel();
wpanel.setBorder(BorderFactory.createTitledBorder("目?源"));
cpanel.add(epanel);
cpanel.add(wpanel);
return cpanel;
}
protected JPanel buildEPanel(){
JPanel sourcePanel = new JPanel();
sourcePanel.setLayout(null);
JLabel jlabel1 = new JLabel("Excel文件");
jlabel1.setBounds(new Rectangle(10,15,60,20));
sourcePanel.add(jlabel1,null);
ExcelFile = new JTextField();
ExcelFile.setBounds(new Rectangle(65,15,140,20));
sourcePanel.add(ExcelFile,null);
JButton selfile = new JButton("??");
selfile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){FileBrowser();}});
selfile.setBounds(new Rectangle(205,15,60,20));
sourcePanel.add(selfile,null);
JLabel jlabel2 = new JLabel("表 ?");
jlabel2.setBounds(new Rectangle(10,40,60,20));
sourcePanel.add(jlabel2,null);
ExcelField = new JComboBox();
ExcelField.setBounds(new Rectangle(65,40,140,20));
sourcePanel.add(ExcelField,null);
JButton openbtn = new JButton("打?");
openbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){FileOpen();}});
openbtn.setBounds(new Rectangle(205,40,60,20));
sourcePanel.add(openbtn,null);
return sourcePanel;
}
protected JPanel buildWPanel(){
JPanel dePanel = new JPanel();
dePanel.setLayout(null);
JLabel jlabel1 = new JLabel("服?器:");
jlabel1.setBounds(new Rectangle(10,15,60,20));
dePanel.add(jlabel1,null);
serverPath = new JTextField("192.168.3.6:5432");
serverPath.setBounds(new Rectangle(65,15,200,20));
dePanel.add(serverPath,null);
JLabel jlabel2 = new JLabel("?据?:");
jlabel2.setBounds(new Rectangle(10,40,60,20));
dePanel.add(jlabel2,null);
database = new JTextField("gaomingdb9");
database.setBounds(new Rectangle(65,40,200,20));
dePanel.add(database,null);
JLabel jlabel3 = new JLabel("用?名:");
jlabel3.setBounds(new Rectangle(10,65,60,20));
dePanel.add(jlabel3,null);
user = new JTextField("postgres");
user.setBounds(new Rectangle(65,65,200,20));
dePanel.add(user,null);
JLabel jlabel4 = new JLabel("密 ?");
jlabel4.setBounds(new Rectangle(10,90,60,20));
dePanel.add(jlabel4,null);
password = new JPasswordField("gaoming");
password.setBounds(new Rectangle(65,90,140,20));
dePanel.add(password,null);
JButton btn = new JButton("?接");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){connectDatabase();}});
btn.setBounds(new Rectangle(205,90,60,20));
dePanel.add(btn,null);
JLabel jlabel5 = new JLabel("?据表名");
jlabel5.setBounds(new Rectangle(10,115,60,20));
dePanel.add(jlabel5,null);
tables = new JComboBox();
tables.setBounds(new Rectangle(65,115,140,20));
dePanel.add(tables,null);
JButton seebtn = new JButton("?示");
seebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){viewTableFields();}});
seebtn.setBounds(new Rectangle(205,115,60,20));
dePanel.add(seebtn,null);
return dePanel;
}
protected JPanel buildSouthJPanel(){
JPanel bottomPanel = new JPanel();
bottomPanel.setBorder(BorderFactory.createEtchedBorder());
JPanel inner1 = new JPanel();
JPanel inner2 = new JPanel();
JPanel inner3 = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(inner1,BorderLayout.NORTH);
bottomPanel.add(inner2,BorderLayout.CENTER);
bottomPanel.add(inner3,BorderLayout.SOUTH);
inner1.setLayout(new GridLayout(1,6));
JLabel jlabel7 = new JLabel("??操作",JLabel.CENTER);
inner1.add(jlabel7);
ExcelCol = new JComboBox();
inner1.add(ExcelCol);
JLabel jlabel8 = new JLabel("<---->",JLabel.CENTER);
inner1.add(jlabel8);
tableFields = new JComboBox();
inner1.add(tableFields);
JButton jlbtn = new JButton("建立??");
jlbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){buildConnect();}});
inner1.add(jlbtn);
JButton delbtn = new JButton("?除??");
delbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){delConnect();}});
inner1.add(delbtn);
inner2.setLayout(new GridLayout());
String colnames[]={"Excel列名","?料表列名"};
Object rowdata[][]=null;
dtm = new DefaultTableModel(rowdata,colnames);
jtable = new JTable(dtm);
JScrollPane jsp = new JScrollPane();
jsp.getViewport().add(jtable);
inner2.add(jsp);
inner3.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton surebtn = new JButton("?入?据");
surebtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){DataImport();}});
JButton cancle = new JButton("返 回");
cancle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){DLGHide();}});
inner3.add(surebtn);
inner3.add(cancle);
return bottomPanel;
}
//Excel文件??
protected void FileBrowser(){
filechooser = new JFileChooser("C://");
int result = filechooser.showOpenDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
file = filechooser.getSelectedFile();
ExcelFile.setText(file.getPath().toString());
try{
wk = Workbook.getWorkbook(file);
String sheets[] = wk.getSheetNames();
ExcelField.removeAllItems();
for(int i=0;i<sheets.length;i++){
ExcelField.addItem(sheets[i]);
}
}
catch(BiffException e){}
catch(IOException e){}
}
}
//Excel文件打?
protected void FileOpen(){
ExcelCol.removeAllItems();
excel= ExcelField.getSelectedItem().toString();
Cell cd[]=wk.getSheet(excel).getRow(0);
for(int i=0;i<cd.length;i++){
ExcelCol.addItem(cd[i].getContents());
}
}
protected void connection(){
String spath = serverPath.getText().trim();
String db = database.getText().trim();
String use = user.getText().trim();
final char str[] = password.getPassword();
final String pwd = new String(str);
String databasepath = "jdbc:postgresql://"+spath+"/"+db;
try{
Class.forName("org.postgresql.Driver");
cn = DriverManager.getConnection(databasepath,use,pwd);
st = cn.createStatement();
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(this,"?据?????失??");
this.dispose();
}
catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(this,"?据??接失?!");
return ;}
}
//?料??接
protected void connectDatabase(){
try{
connection();
String sql = "select * from pg_stat_user_tables";
rs = st.executeQuery(sql);
while(rs.next()){
tables.addItem(rs.getString("relname"));
}
rs.close();
st.close();
cn.close();
}
catch(SQLException e){
e.printStackTrace();
JOptionPane.showMessageDialog(this,"?据??行失?!");
return ;}
}
//?示?料表中的有效列名
protected void viewTableFields(){
connection();
seltable = tables.getSelectedItem().toString();
String sql = " select * from "+ seltable;
try{
tableFields.removeAllItems();
rs = st.executeQuery(sql);
rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
for(int i=1;i<=colnum;i++){
tableFields.addItem(rsmd.getColumnName(i));
}
rs.close();
st.close();
cn.close();
}
catch(SQLException e){}
}
//建立??
protected void buildConnect(){
String sdata = ExcelCol.getSelectedItem().toString();
String ddata = tableFields.getSelectedItem().toString();
Object rowdata[]={sdata,ddata};
dtm.addRow(rowdata);
}
//?除??
protected void delConnect(){
int nrow = jtable.getSelectedRow();
if(nrow<0){
JOptionPane.showMessageDialog(this,"??需?除的??!");
return ;
}
dtm.removeRow(nrow);
}
//?据?料的?入
protected void DataImport(){
Sheet selsheet = wk.getSheet(excel);
int num = jtable.getRowCount();
String[] totalcount = new String[num];
String[] tf = new String[num];
connection();
try{
// cn.setAutoCommit(false);
for (int i = 1; i < selsheet.getRows(); i++) {
int j = 0;
for (j = 0; j < jtable.getRowCount(); j++) {
String ecol = jtable.getValueAt(j, 0).toString();
int d = selsheet.findCell(ecol).getColumn(); //Excel中的列
totalcount[j] = selsheet.getCell(d, i).getContents();
tf[j] = jtable.getValueAt(j, 1).toString();
}
String sql = "INSERT INTO " + seltable + "(";
String value = ") VALUES(";
for (int k = 0; k < j; k++) {
sql = sql + tf[k] + ",";
value = value + totalcount[k] + ",";
}
try {
int d = sql.lastIndexOf(",");
sql = sql.substring(0, d);
d = value.lastIndexOf(",");
value = value.substring(0, d);
String str = sql + value + ")";
System.out.println(str);
st.executeUpdate(str);
}
catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "?入失?");
//cn.rollback();
return;
}
}
rs.close();
st.close();
cn.close();
JOptionPane.showMessageDialog(this, "?入成功");
}
catch(SQLException e){e.printStackTrace();}
}
protected void DLGHide(){
System.exit(0);
}
public static void main(String args[]){
new ImportData();
}
}