JAVA中数据库中的表显示问题
2016-06-06 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
修改了一下你的代码:添加了一次查询,先查询出总记录数,然后再查询结果,你的问题是先循环滚动了一下结果集求出总记录数,如果再循环 rs.next()就取不到值了。
解决方法有几种,我给你一个简单的解决方案:
package org.somken;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
public class Test extends JFrame {
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
public Test() {
super("JTable数据绑定示例");
this.setSize(330, 400);
this.setLayout(null);
this.setLocation(100, 50);
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10, 50, 300, 270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10, 10, 300, 30);
this.btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
btnShow_ActionPerformed(ae);
}
});
add(this.scpDemo);
add(this.btnShow);
this.setVisible(true);
}
public void btnShow_ActionPerformed(ActionEvent ae) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/student", "root", "");
Statement pstm = conn.createStatement();
String sql1 = "select count(*) from st1";
String sql2 = "select * from st1";
ResultSet rs1 = pstm.executeQuery(sql1);
int count = 0;
//得到总记录数
if (rs1.next()) {
count=rs1.getInt(1);
}
//关闭第一个结果集
rs1.close();
Object[][] info = new Object[count][5];
count = 0;
//查询
ResultSet rs2 = pstm.executeQuery(sql2);
while (rs2.next()) {
info[count][0] = rs2.getString("no");
info[count][1] = rs2.getString("name");
info[count][2] = rs2.getString("sex");
info[count][3] = rs2.getString("birthday");
info[count][4] = rs2.getString("speciality");
count++;
}
for(Object []o:info){
System.out.println(o[0]);
System.out.println(o[1]);
System.out.println(o[2]);
}
String[] title = { "学号", "姓名", "年龄", "性别", "专业" };
// 创建JTable
this.tabDemo = new JTable(info, title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
} catch (Exception sqle) {
JOptionPane.showMessageDialog(null, "错误:"+sqle.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new Test();
}
}
解决方法有几种,我给你一个简单的解决方案:
package org.somken;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
public class Test extends JFrame {
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
public Test() {
super("JTable数据绑定示例");
this.setSize(330, 400);
this.setLayout(null);
this.setLocation(100, 50);
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10, 50, 300, 270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10, 10, 300, 30);
this.btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
btnShow_ActionPerformed(ae);
}
});
add(this.scpDemo);
add(this.btnShow);
this.setVisible(true);
}
public void btnShow_ActionPerformed(ActionEvent ae) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/student", "root", "");
Statement pstm = conn.createStatement();
String sql1 = "select count(*) from st1";
String sql2 = "select * from st1";
ResultSet rs1 = pstm.executeQuery(sql1);
int count = 0;
//得到总记录数
if (rs1.next()) {
count=rs1.getInt(1);
}
//关闭第一个结果集
rs1.close();
Object[][] info = new Object[count][5];
count = 0;
//查询
ResultSet rs2 = pstm.executeQuery(sql2);
while (rs2.next()) {
info[count][0] = rs2.getString("no");
info[count][1] = rs2.getString("name");
info[count][2] = rs2.getString("sex");
info[count][3] = rs2.getString("birthday");
info[count][4] = rs2.getString("speciality");
count++;
}
for(Object []o:info){
System.out.println(o[0]);
System.out.println(o[1]);
System.out.println(o[2]);
}
String[] title = { "学号", "姓名", "年龄", "性别", "专业" };
// 创建JTable
this.tabDemo = new JTable(info, title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
} catch (Exception sqle) {
JOptionPane.showMessageDialog(null, "错误:"+sqle.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询