2024-04-21 00:36:19
Vector 这种方式添加表格数据,我没用过,我给你改了一下。你看一下,这个在你那里运行能不能成功?
有问题再联系。
/**
* 从oracle数据库中取出学生信息
*/
package com.test3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Test2 extends JFrame {
// rowDate用来存放行数据,columnNames存放行数据
DefaultTableModel model = null;
JTable jt;
JScrollPane jsp = null;
// 定义操作数据库需要的东西
PreparedStatement ps = null;
Connection ct = null;
ResultSet rs = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2 test2 = new Test2();
}
public Test2() {
// 设置列明
String[] columnNames = { "学号", "名字", "性别", "籍贯", "系别" };
// 初始化JTable
// ------------------从这里就开始出险问题了
model = new DefaultTableModel(columnNames, 0);
jt = new JTable(model);
// 初始化jsp JScrollPane
jsp = new JScrollPane(jt);
addRow();
// 把jsp放到jframe
this.add(jsp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void addRow() {
try {
// 1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
ct = DriverManager
.getConnection("jdbc:ORACLE:thin:@localhost:1521:oracle",
"scott", "tiger");
ps = ct.prepareStatement("select * from stu");
rs = ps.executeQuery();
while (rs.next()) {
// rowDate可以存放多行
Vector hang = new Vector();
String[] row = { rs.getString("stuId"),
rs.getString("stuName"), rs.getString("stuSex"),
rs.getString("stuJg"), rs.getString("stuDEPT") };
// 加入到rowdate
model.addRow(row);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("数据库连接失败" + e.getMessage());
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (ct != null)
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败" + e.getMessage());
}
}
}
}
2024-04-21 00:36:03
2024-04-21 00:26:03
可以指出来吗?我应该怎修改那。谢谢您的回答
你点下错误的行数,看看是跳到具体的哪一行,发给我看看。
2024-04-21 00:47:51
2024-04-21 01:17:09