该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下
import java.sql.*;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.sql.*;
//从数据库中取出
public class Test2 extends JFrame{
//rowData用来存放行数据
Vector rowData,columnNames;
JTable jt=null;
JScrollPane jsp=null;
//定义数据需要的东西
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
public static void main(String[] args) {
Test2 test2=new Test2();
}
public Test2(){
columnNames=new Vector();
//设置列名
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
rowData=new Vector();
try {
//1加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
ct=DriverManager.getConnection("jdbc:oracle:thin:@10.58.0.197:1521:orclhsp","scott","orcl");
ps=ct.prepareStatement("select * from stu");
System.out.println(ps);
rs=ps.executeQuery();
while(rs.next())
{
//rowData可以存放多行
Vector hang=new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
hang.add(rs.getString(2));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.cancel();
if(ct!=null) ct.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//初始化JTable;
jt=new JTable(rowData,columnNames);
jsp=new JScrollPane(jt);
this.add(jsp);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}