5 Replies - 911 Views - Last Post: 04 December 2012 - 11:15 AM Rate Topic: -----

#1 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

unable to get database table values into jtable on displayPane3

Posted 03 December 2012 - 09:37 AM


import java.awt.*;
import java.awt.GridLayout.*;
import java.awt.BorderLayout.*;
import javax.swing.*;
import javax.swing.JPanel.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;


//the Student main screen
public class Screen1 
{

private JPanel listItemsPane1, buttonsPane2,displayPane3; 
private JLabel l[];
private JButton jbutton1,jbutton2,jbutton3,jbutton4;
private JComboBox course, year, section;
private final String c[]= { "MCA", "MBA" };
private final String y[]= { "1st", "2nd", "3rd" }; 
private final String s[]= { "A", "B", "C", "D" };
private TextField tf;
private JTable table;
private Vector columnNames = new Vector();
private Vector data = new Vector();


//private Container c=getContentPane();

//other class
private table tab;
private int i;
Connection con;
PreparedStatement st;
ResultSet rs;

//initialize components
public Screen1()
{
JFrame sf1= new JFrame();

// set up panel
listItemsPane1= new JPanel();
buttonsPane2= new JPanel();
displayPane3= new JPanel();
listItemsPane1.setBorder(BorderFactory.createTitledBorder("Top"));
buttonsPane2.setBorder(BorderFactory.createTitledBorder("Bottom"));
displayPane3.setBorder(BorderFactory.createTitledBorder("Display"));




l= new JLabel[4];
l[0]= new JLabel("Course:");
l[1]= new JLabel("Year:");
l[2]= new JLabel("Section:");
l[3]= new JLabel("HT.No");

jbutton1= new JButton("OK");
jbutton2= new JButton("New");
jbutton3= new JButton("Edit");
jbutton4= new JButton("Delete");

tf= new TextField(12);

course= new JComboBox(c);
course.setMaximumRowCount(2);
year= new JComboBox(y);
year.setMaximumRowCount(3);
section= new JComboBox(s);
section.setMaximumRowCount(4);

//table=new JTable();
table=new JTable(data, columnNames);
JScrollPane scroll=new JScrollPane(table);
displayPane3.add(scroll);





//add listeners to components
/*
jbutton1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String ht=tf.getText();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
con=DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar");
System.out.println("connection success");
System.out.println(ht);

String query="SELECT * from std_details where HTNO=?";
PreparedStatement st=con.prepareStatement(que);
st.setString(1,ht);
rs=st.executeQuery();
while(rs.next()){
String htno=rs.getString("htno");
String name=rs.getString("sname");
String m=rs.getString("mob");
String mail=rs.getString("email");
String cou=rs.getString("course");
String yr=rs.getString("year");
String sec=rs.getString("section");
String sex=rs.getString("gender");

JOptionPane.showMessageDialog (null, "Htno:-"+ht+"\nName:-"+name+"\nCourse:-"+cou+"\nSec:-"+sec+"\nYear:-"+yr+
"\nMobile:-"+m+"\nMail:-"+mail, 
"Student Info.", JOptionPane.INFORMATION_MESSAGE);
System.out.println(name);
}
//System.out.println(htno);
st.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Retrieval failed");
}
}
}
); 
*/


jbutton1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Vector columnNames = new Vector();
Vector data = new Vector();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
con=DriverManager.getConnection("jdbc:odbc:oradsn","system","kumar");
String sql = "Select * from std_details where htno=?";
PreparedStatement st = con.prepareStatement(sql);
//ResultSet rs = st.executeQuery();
String ht=tf.getText();
st.executeUpdate();
System.out.println("hw ru");

ResultSetMetaData metaData = rs.getMetaData();

int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}

while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
st.close();
}//try closed
 
catch (Exception e) {System.out.println(e);}
new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i < table.getColumnCount(); i++) {
column = table.getColumnModel().getColumn(i);
//column.setMaxWidth(100);
}



}
}
); 




jbutton2.addActionListener(
new ActionListener() // listener for new event
{
public void actionPerformed(ActionEvent ae)
{
new New(); // calling the new class
}
}
);

jbutton3.addActionListener(
new ActionListener() // listener for edit event
{
public void actionPerformed(ActionEvent ae)
{
new Edit();// calling the edit class
//new New();
}
}
);

jbutton4.addActionListener(
new ActionListener() // listener for delete event
{
public void actionPerformed(ActionEvent ae)
{
}
}
);

course.addItemListener(
new ItemListener()// course item listener
{
public void itemStateChanged(ItemEvent ie)
{
}
}
);

//set layout and add compnents to panel 1

listItemsPane1.setLayout(new FlowLayout());
listItemsPane1.add(l[0]);
listItemsPane1.add(course);
listItemsPane1.add(l[1]);
listItemsPane1.add(year);
listItemsPane1.add(l[2]);
listItemsPane1.add(section);
listItemsPane1.add(l[3]);
listItemsPane1.add(tf);
listItemsPane1.add(jbutton1);

//set layout and add compnents to panel 2
buttonsPane2.setLayout(new FlowLayout());
buttonsPane2.add(jbutton2);
buttonsPane2.add(jbutton3);
buttonsPane2.add(jbutton4);







sf1.getContentPane();
sf1.setLayout(new GridLayout(3,1));
sf1.add(listItemsPane1);
sf1.add(displayPane3);
sf1.add(buttonsPane2);
sf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//getContentPane.add(sf1);
sf1.setSize(600,300);
sf1.setVisible(true);

}

public static void main(String[] ar)
{
Screen1 scr= new Screen1();
}
}






Is This A Good Question/Topic? 0
  • +

Replies To: unable to get database table values into jtable on displayPane3

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: unable to get database table values into jtable on displayPane3

Posted 03 December 2012 - 09:43 AM

Site Questions & Support is for questions about DREAM.IN.CODE, not YOUR CODE!

Moved to Java.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: unable to get database table values into jtable on displayPane3

Posted 03 December 2012 - 10:36 AM

Here is a quick one that should do the job

http://www.dreaminco...snippet6437.htm
Was This Post Helpful? 0
  • +
  • -

#4 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: unable to get database table values into jtable on displayPane3

Posted 03 December 2012 - 09:02 PM

hello pbl, thanks for quick response, i'm new to swings and my requirement is to populate the data from database and show it in one of the jpanel, i'm getting everything but unable to display the data on the jtable, it is showing the result in command prompt, but not on jtable.

from database it must populate table data to jtable on PANEL named Displaypane3

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#5 balraj8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-October 12

Re: unable to get database table values into jtable on displayPane3

Posted 03 December 2012 - 09:31 PM

sir i tried my previous code, but the run time error is as follows:

java.sql.exception:[oracle][odbc][ora]ora-01008: not all variables bound
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: unable to get database table values into jtable on displayPane3

Posted 04 December 2012 - 11:15 AM

From my posted snippet just do

TableModelFromRS tm = new TableModelFromRS(yourResultSet);
JTable table = new JTable™;
panel.add(new JScrollPane(table));
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1