3

I am using the following code for retrieving the data from MySQL Database. The code connects to the database properly. My problem is in retrieving the data from MySQL DB and showing it in the JTable.

package student;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;

public class student1 extends javax.swing.JFrame {
    // public Connection Conn;
    Vector data = new Vector() ;
    Vector columnNames= new Vector();

    public student1() {
        initComponents();
        Connection conn;
    }

    public void Connection(){
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String username = "root";
            String password = "root";
            String Database = "jdbc:mysql://localhost:3306/project";
            Connection conn = DriverManager.getConnection(Database, username, password);
            System.out.println("*** Connect to the database ***");
            String Query = "Select * from  StudentMaster";
            Statement smnt = conn.createStatement();
            ResultSet results = smnt.executeQuery( Query );
            ResultSetMetaData metaDt = results.getMetaData();
            System.out.println(metaDt);

            int cols = metaDt.getColumnCount();
            System.out.println("database" +cols);
            for(int i=1;i<cols;i++){
                columnNames.addElement (metaDt.getColumnName(i));
            }
            while(results.next()){
                Vector row= new Vector(cols);
                for(int i=1;i<=cols;i++){
                    row.addElement(results.getObject(i));
                }
                data.addElement(row);
            }
            results.close();
            smnt.close();
            conn.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "StudId", "StudNo", "StudName", "StudClass", "StudMrk1", "StudMrk2", "StudTot", "StudRes"
            }
        ){
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(212, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new student1().setVisible(true);
               student1 s1=new student1();
               s1.Connection();

            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration
}

When this code is I executed, I get the following output. How can I solve my problem?

*** Connect to the database ***
com.mysql.jdbc.ResultSetMetaData@1194a4e - Field level information: 
        com.mysql.jdbc.Field@15d56d5[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudId,originalColumnName=StudId,mysqlType=3(FIELD_TYPE_LONG),flags= PRIMARY_KEY, charsetIndex=63, charsetName=US-ASCII]
        com.mysql.jdbc.Field@efd552[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudNo,originalColumnName=StudNo,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
        com.mysql.jdbc.Field@19dfbff[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudName,originalColumnName=StudName,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
        com.mysql.jdbc.Field@10b4b2f[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudClass,originalColumnName=StudClass,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
        com.mysql.jdbc.Field@750159[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudMrk1,originalColumnName=StudMrk1,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
        com.mysql.jdbc.Field@1abab88[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudMrk2,originalColumnName=StudMrk2,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
        com.mysql.jdbc.Field@18a7efd[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudTot,originalColumnName=StudTot,mysqlType=3(FIELD_TYPE_LONG),flags=, charsetIndex=63, charsetName=US-ASCII]
        com.mysql.jdbc.Field@1971afc[catalog=project,tableName=StudentMaster,originalTableName=studentmaster,columnName=StudRes,originalColumnName=StudRes,mysqlType=253(FIELD_TYPE_VAR_STRING),flags=, charsetIndex=8, charsetName=Cp1252]
database8
BUILD SUCCESSFUL (total time: 14 seconds)
1
  • 1
    please learn java naming conventions and stick to them Commented Dec 26, 2011 at 11:49

2 Answers 2

3

I don't see any errors. What you see are the messages that are being printed from within your code, primarily from these statements:

ResultSetMetaData metaDt = results.getMetaData();
System.out.println(metaDt);
int cols = metaDt.getColumnCount();
System.out.println("database" +cols);
Sign up to request clarification or add additional context in comments.

5 Comments

I store some data in that particular data Base .I cant retrieve that all contents to JTable.
Please update your question. From what you have written it seems that you are getting an error but there isn't one. You haven't written the code that add the data that you retrieved from the database to the JTable. Are you asking how to add data to the JTable? If so, then please state that clearly in your question.
I create Jtable in netbeans and customize that for my own need.like StudName,StudId,StudMrk1 and so on.I use the this code to retrieve the data from MYSQL to JTABLE?when I use this code i can't get display that DB values in JTable.
@Abbas What is the output of this code?: System.out.println(metaDt);
It is most probably "com.mysql.jdbc.ResultSetMetaData@1194a4e - Field level information: " and the lines indented after it show the ResultSet metadata information.
0

Your basic code looks reasonable. You create two Vectors containing the columns names and data, but no where do you then use these Vectors to create a DefaultTableModel which can then be added to the table.

So your code should be something like:

conn.close();
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table.setModel( model );

For a complete example (using Access) check out the Table From Database Example found in Table From Database.

Comments

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.