java中数据库中的表显示问题
importjavax.swing.*;importjavax.swing.table.JTableHeader;importjava.awt.event.ActionE...
import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.sql.*;
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
{
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","");
Statement pstm = conn.createStatement();
String sql = "select * from st1";
ResultSet rs = pstm.executeQuery(sql);
int count = 0;
while(rs.next())
{
count++;
}
Object[][] info = new Object[count][5];
count = 0;
while(rs.next())
{
info[count][0] = rs.getString("no");
info[count][1] = rs.getString("name");
info[count][2] = rs.getString("sex");
info[count][3] = rs.getString("birthday");
info[count][4] = rs.getString("speciality");
count++;
}
String[] title = {"学号","姓名","年龄","性别","专业"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}
catch(SQLException sqle)
{
JOptionPane.showMessageDialog(null,"错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new Test();
}
}
数据库是mysql,中的一个数据库是student,student中的表示st1,表中的数据有no,name,sex,birthday,speciality这五个,但运行之后却显示不出来,请大家帮忙看一下啊(程序有一些是复制网上的):
运行之后就这样了,为啥呢。 展开
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.sql.*;
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
{
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","");
Statement pstm = conn.createStatement();
String sql = "select * from st1";
ResultSet rs = pstm.executeQuery(sql);
int count = 0;
while(rs.next())
{
count++;
}
Object[][] info = new Object[count][5];
count = 0;
while(rs.next())
{
info[count][0] = rs.getString("no");
info[count][1] = rs.getString("name");
info[count][2] = rs.getString("sex");
info[count][3] = rs.getString("birthday");
info[count][4] = rs.getString("speciality");
count++;
}
String[] title = {"学号","姓名","年龄","性别","专业"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}
catch(SQLException sqle)
{
JOptionPane.showMessageDialog(null,"错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args)
{
new Test();
}
}
数据库是mysql,中的一个数据库是student,student中的表示st1,表中的数据有no,name,sex,birthday,speciality这五个,但运行之后却显示不出来,请大家帮忙看一下啊(程序有一些是复制网上的):
运行之后就这样了,为啥呢。 展开
展开全部
修改了一下你的代码:添加了一次查询,先查询出总记录数,然后再查询结果,你的问题是先循环滚动了一下结果集求出总记录数,如果再循环 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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询