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(); } }
↧
unable to get database table values into jtable on displayPane3
↧