怎么用JAVA链接数据库ORACLE实现导出一个表

2025-12-06 15:09:03
推荐回答(1个)
回答1:

你可以用 swing 或 jsp 来做:

swing:

我写的样例:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.awt.*;
import javax.swing.*;

public class data extends JFrame implements ActionListener{
JButton select,print;
JTable table;
Object body[][]=new Object[50][4];
String title[]={"编号","用户名","密码","邮箱"};
Connection conn;
Statement stat;
ResultSet rs;
JTabbedPane tp;
public data() {
super("用户信息");
this.setSize(700,500);
this.setLocation(500,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel ps=new JPanel();
select=new JButton("查询");
print=new JButton("打印预览");
select.addActionListener(this);
print.addActionListener(this);
ps.add(select);
ps.add(print);
table=new JTable(body,title);
tp=new JTabbedPane();
tp.add("t41c_user表",new JScrollPane(table));
this.getContentPane().add(tp,"Center");
this.getContentPane().add(ps,"South");
this.setVisible(true);
this.connection();

}
public void connection(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@*.*.*.*:1521:orcl";
conn=DriverManager.getConnection(url,"username","password");
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
}
}
public static void main(String[] args) {
data data= new data();
}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==select)
{select();}
else if(e.getSource()==print)
{print();}

}
public void print()
{}
public void select() {
try {
for(int x=0;x body[x][0]=null;
body[x][1]=null;
body[x][2]=null;
body[x][3]=null;
}
int i=0;
rs=stat.executeQuery("select * from t41c_user");
while(rs.next()){
body[i][0]=rs.getInt(1);
body[i][1]=rs.getString(2);
body[i][2]=rs.getString(3);
body[i][3]=rs.getString(4);
i=i+1;
}
this.repaint();
} catch (SQLException ex) {
}

}

}


jsp:

比较简单,可以百度看看,有很多不错的例子。