Hi, Here is my code:
import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class civil_fst extends JFrame implements ItemListener, ActionListener{ static Connection con; static Statement stmt; static ResultSet rs; static ResultSetMetaData rsMeta; String query; String ref="",rollno=""; String[] heads; int[] stChange; String[][] cubeData; int rows, cols, coo; /** Creates new form civil_fst */ public civil_fst() { initComponents(); con = null; stmt = null; rs = null; rsMeta=null; query="SELECT * FROM civilfattnd order by Regd_No"; rows=0; cols=0; coo=0; try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root"); con.setAutoCommit(true); stmt=con.createStatement(); rs=stmt.executeQuery(query); //execute the query and get resultset(rs) rsMeta=rs.getMetaData(); //get rs metadata cols=rsMeta.getColumnCount(); // get number of columns coo=cols-1; while(rs.next()) { // get rows in resultset ++rows; } heads = new String[cols]; // initialize col heads for (int i = 0; i < cols; i++) { // getting colums heading in heads[] int colindex=i+1; // column indexes start from 1 heads[i] = rsMeta.getColumnName(colindex); } stChange = new int[rows]; //to save value when status changed rs=stmt.executeQuery(query); // execure query and get resultset (rs) cubeData = new String[rows][cols]; // initialize table data int j=0; int k=0; // function to get resultset data while(rs.next()) { if(j<=rows) { int h=1; if(k<=cols) { for(int cc=0;cc<cols; cc++) { cubeData[j][k]=rs.getString(h); h++; k++; } } k=0;h=0; j++; } } } catch(Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(System.err); } cp.setLayout(new GridLayout(rows+2,cols)); // 1 additional Row of Col heading, 1 for save btn //---------------- Adding heading in CP--------------------------------------- for (int i = 0; i < cols; i++) { JLabel jl = new JLabel(" [ "+heads[i]+" ] "); jl.setOpaque(true); jl.setBackground(Color.gray); jl.setForeground(Color.white); cp.add(jl); } //---------------- Adding data --------------------------------------- for(int j=0; j<rows; j++){ for(int k=0; k<cols; k++) { if(k==(cols-1)) { ref="-"+j+"."+k; String status=cubeData[j][k]+ref; stChange[j]=Integer.parseInt(rollno); // save previously selected status of each // row(student) + array address Choice jc = new Choice(); jc.add("Present"+ref); jc.add("Leave"+ref); jc.add("Absent"+ref); jc.select(status); jc.addItemListener(this); cp.add(jc); } else cp.add(new JLabel(cubeData[j][k])); rollno=cubeData[j][k]; } } addSaveButton(); } //------------------------SAVE BUTTON----------------------------------------- public void addSaveButton(){ JButton saveBtn = new JButton(" :::: Save :::: "); saveBtn.addActionListener(this); cp.add(saveBtn); } //------------------------Item Listener ----------------------------------------- public void itemStateChanged(ItemEvent ie) { String s = (String)ie.getItem(); String ro = s.substring(s.indexOf("-")+1,s.indexOf(".")); String co = s.substring(s.indexOf(".")+1,s.length()); String stats = s.substring(0,s.indexOf("-")); int roo = Integer.parseInt(ro); coo = Integer.parseInt(co); cubeData[roo][coo]=stats; } //------------------------Action Listener----------------------------------------- public void actionPerformed(ActionEvent ae) { try{ String create="CREATE TABLE civ_fourth (`Name` VARCHAR(50), `Regd_No` INT, `Status` VARCHAR(20) NOT NULL)"; con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root"); stmt.executeUpdate(create); //con.close(); } catch(Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(System.err); } try{ for(int j=0; j<rows; j++) { //String updt = "update civilfattnd set Status= '"+cubeData[j][coo]+"' where Regd_No="+stChange[j]; String ins="INSERT INTO civ_fourth (`Regd_No`, `Status`) VALUES('"+stChange[j]+","+cubeData[j][coo]+")"; con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root"); //stmt.executeUpdate(updt); PreparedStatement ps=con.prepareStatement(ins); } //this.con.commit(); con.setAutoCommit(true); System.out.println("Records updated..!"); con.close(); }catch(Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(System.err); } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { cp = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("...:::Civil First Sem:::..."); setBounds(new java.awt.Rectangle(250, 250, 0, 0)); setLocationByPlatform(true); cp.setBackground(new java.awt.Color(154, 95, 125)); javax.swing.GroupLayout cpLayout = new javax.swing.GroupLayout(cp); cp.setLayout(cpLayout); cpLayout.setHorizontalGroup( cpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 539, Short.MAX_VALUE) ); cpLayout.setVerticalGroup( cpLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 308, Short.MAX_VALUE) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new civil_fst().setVisible(true); } }); }
But this code create a new table but problem is its not execute the query for insert. I tried a lot but not successes . Please help me
Ads