2

I am trying to display data from the database in java using Jtable, yes I have achieved that but now the conflict is the surname and name they are displayed in one Column yet I would like them to be displayed in different columns . below is my code please help ..

import java.awt.Dimension;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ju {
    private static Object request;
    static JTable mysTable;
    //constructor method

    public static void main (String args []){
    String [] columnNames = {"Name","Lastname","Id","Style"};
        mysTable = new JTable(4,4);
        mysTable.setBounds(20,10,300,300);

        JFrame frame = new JFrame("King Musa Saloon Software");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(500,500);
        frame.setResizable(false);
           frame.setVisible(true);
            frame.add(mysTable);

//frame.add(mysTable);
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loading success!");
            String url = "jdbc:mysql://localhost:3306/saloon";
            String name = "root";
            String password = "";
            try {


                java.sql.Connection con = DriverManager.getConnection(url, name, password);
                System.out.println("Connected.");
         // pull data from the database 
java.sql.Statement stmts = null;
String query = "select  userid, username, name from saloonuser ";
stmts = con.createStatement();
ResultSet rs = stmts.executeQuery(query);
int li_row = 0;
while(rs.next()){
    mysTable.setValueAt(rs.getString("username"),li_row,0);
    mysTable.setValueAt(rs.getString("name"),li_row,0);
    int userid = rs.getInt("userid");
    String username = rs.getString("username");
    String name1     = rs.getString("name");
    System.out.println(name1);
    li_row++;


}




            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}
1
  • Take a look at How to use tables. You should be adding rows to the table model instead of trying to update the cells via the table model, as the rows may not exists Commented Jun 2, 2013 at 21:42

1 Answer 1

6

Take a look at your code here:

mysTable.setValueAt(rs.getString("username"),li_row,0);
mysTable.setValueAt(rs.getString("name"),li_row,0);

It's writing in the same column, you should change it to:

mysTable.setValueAt(rs.getString("username"),li_row,0);
mysTable.setValueAt(rs.getString("name"),li_row,1);

Try and see if it work :)

Sign up to request clarification or add additional context in comments.

6 Comments

In life there are people whom we call super heroes , you one of them
How can I add headings of each column please @A-SM
I usually use this, look at your code here: String [] columnNames = {"Name","Lastname","Id","Style"}; mysTable = new JTable(4,4); I convert it to: Object [] columnNames = {"Name","Lastname","Id","Style"}; mysTable = new JTable(columnNames,4);
So should I use this only Object [] columnNames = {"Name","Lastname","Id","Style"}; mysTable = new JTable(columnNames,4); @A-SM
it says constructor JTable(Object int) undefined
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.