java- mysql data retriving problem
import java.sql.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.Vector;
public class SimpleTable extends JFrame {
public SimpleTable() {
super开发者_StackOverflow中文版("Simple JTable Test");
setSize(350, 200);
//setLocation(250,300);
int ColCount;
int i;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/employ_details","root","");
java.sql.Statement stmt= con.createStatement();
ResultSet rs= stmt.executeQuery("select * from employe_details ");
ResultSetMetaData meta =rs.getMetaData();
ColCount=meta.getColumnCount();
String s1 = null;
String s2 = null;
String s3 = null;
String s4 = null;
String s5 = null;
String s6 = null;
String s7 = null;
while (rs.next())
{
String [] record= new
String[ColCount];
for (i=0; i<ColCount; i++)
{
record[i]=rs.getString(i+1);
s1= rs.getString("Employee_ID");
s2= rs.getString("Employee_Name");
s3= rs.getString("Contact_Number");
s4 = rs.getString("Address");
s5 = rs.getString("Email");
s6 = rs.getString("dob");
s7 = rs.getString("Sex");
}
JTable jt = new JTable(new String[][]{{s1,s2,s3,s4,s5,s6,s7}}, new String[]{"Employee_ID","Employee_Name","Contact_Number","Address","Email","dob","Sex"});
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp,BorderLayout.CENTER);
}}
catch (Exception e)
{
System.out.println("failure to connect " + e);
return; //(exit(1));
}
}
public static void main(String args[]) {
SimpleTable st = new SimpleTable();
st.setVisible(true);
}
}
its only shopwing the last row in the database . how i get all the rows in the database to the JTable ?
Your code is reading all the data but it doesn't store all, it holds data of last loop execution only.
And creating table from last row so it will show last row only.
Try something like
List<String> data = new ArrayList<String>();
than
while (rs.next())
{
String [] record= new
String[ColCount];
for (i=0; i<ColCount; i++)
{
record[i]=rs.getString(i+1);
data.add(rs.getString("Employee_ID"));
data.add(rs.getString("Employee_Name"));
.
.
.
data.add(rs.getString("Sex"));
}
JTable jt = new JTable(new String[][]{data.toArray()}, new String[]{"Employee_ID","Employee_Name","Contact_Number","Address","Email","dob","Sex"});
You are iterating through the ResultSet
and no storing all the values of returned by the ResultSet
.
Your s1
..s7
, therefore, holds the last value from the resultset. You can either have s1[]
..s7[]
to hold all values, per column, from the resultset or have a List of all values in the set.
Best way, is to create a POJO object to map your entity (POJO) to your SQL table.
This is an easier problem if you break it into smaller steps.
Instead of putting all your code into one main method, I'd recommend breaking them into several smaller classes. Focus on one, get it working, then move onto the next thing.
Your first problem is data retrieval.
Java's an object-oriented language: start with an Employee class.
package model;
public class Employee
{
private String id;
private String name;
private String phone;
private String address;
private String email;
private Date birthday;
private Gender gender;
// Constructors, getters/setters, equals, hashCode, toString and behavior follow.
}
Once you have that written and tested, write a data access object to deal with the database:
package persistence;
public interface EmployeeDao
{
List<Employee> find();
Employee find(String id);
List<Employee findByName(String name);
void save(Employee e);
void update(Employee e);
void delete(Employee e);
}
Write an implementation for this and get it tested.
Only worry about the JTable when you've got these working. Your problems will be isolated to the UI at that point, because you've got the persistence sorted out.
精彩评论