Blockquote
hi, here is my code, i want code for store data into mysql database when click on submit button from access data from this form, please provide code,
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.util.Calendar; import java.text.SimpleDateFormat; import javax.swing.BorderFactory; import javax.swing.border.Border; import javax.swing.border.TitledBorder; import javax.swing.text.*; import javax.swing.JOptionPane; import java.text.*; import java.util.Date; import javax.swing.JTextField; class start extends JFrame implements ActionListener { JButton bback,bexit,bsub; JLabel linfo,linfo1,linfo2,linfo3,linfo4,linfo5,linfo6,linfo7,linfo8,linfo9,linfo10,linfo11,linfo12,linfo13,linfo14,linfo15,linfo16,linfo17,linfo18,linfo19,linfo20,linfo21; JTextField t1,t2,t3,t4,t5,t6,t7,t8,t9; JFormattedTextField tft3; JList lst1; JComboBox box1,box2,box3,box4,box5,box6,box7,box8,box9,box10,box11,box12; start() { super("GREENVALLYINN"); setSize(1024,768); setVisible(true);
setLayout(null); Container c=getContentPane(); c.setBackground(Color.green); c.setLayout(null); Calendar cal =Calendar.getInstance(); SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdft=new SimpleDateFormat("HH:mm:ss"); Date date=new Date(); String dt=sdf.format(date); String ti=sdft.format(date); linfo=new JLabel("WELCOME TO GREENVALLY IN",JLabel.CENTER); linfo.setBounds(400,5,500,20); linfo.setFont(new Font("Arial",Font.BOLD,20)); Border border = BorderFactory.createLineBorder(Color.red,3); linfo.setBorder(border); c.add(linfo); linfo1=new JLabel("Date"); linfo1.setBounds(10,30,30,30); c.add(linfo1); t1=new JTextField(20); t1.setBounds(35,35,70,20); t1.setMaximumSize(t1.getPreferredSize()); t1.setText(dt); c.add(t1); linfo2=new JLabel("CHECK IN TIME",JLabel.CENTER); linfo2.setBounds(512,30,100,30); c.add(linfo2); t2=new JTextField(20); t2.setBounds(610,35,70,20); t2.setMaximumSize(t1.getPreferredSize()); t2.setText(ti); c.add(t2); linfo3=new JLabel("RECEIPT NO",JLabel.RIGHT); linfo3.setBounds(900,30,100,30); c.add(linfo3); t3=new JTextField(20); t3.setBounds(1000,35,150,20); t3.setMaximumSize(t1.getPreferredSize()); c.add(t3); linfo4=new JLabel("NAME"); linfo4.setBounds(10,60,100,30); c.add(linfo4); t4=new JTextField(30); t4.setBounds(45,65,200,20); t4.setMaximumSize(t1.getPreferredSize()); t4.setFocusable(true); c.add(t4); linfo5=new JLabel("FROM"); linfo5.setBounds(512,60,100,30); c.add(linfo5); box1=new JComboBox(); box1.addItem("VISAKHAPATNAM"); box1.addItem("ANAKAPALLI"); box1.addItem("GAJUWAKA"); box1.addItem("S.KOTA"); box1.addItem("PADERU"); box1.addItem("CHODAVARAM"); box1.addItem("OTHER"); box1.setBounds(600,60,200,30); c.add(box1); linfo6=new JLabel("ADDRESS"); linfo6.setBounds(900,60,100,30); c.add(linfo6); t5=new JTextField(30); t5.setBounds(965,65,200,20); t5.setMaximumSize(t5.getPreferredSize()); c.add(t5); linfo7=new JLabel("CONTACT NO"); linfo7.setBounds(10,100,100,30); c.add(linfo7); t6=new JTextField(10); t6.setBounds(85,105,100,20); t6.setMinimumSize(t6.getPreferredSize()); c.add(t6); linfo8=new JLabel("NO OF PERSONS"); linfo8.setBounds(512,100,100,30); c.add(linfo8); box2=new JComboBox(); box2.addItem("0"); box2.addItem("1"); box2.addItem("2"); box2.addItem("3"); box2.addItem("4"); box2.addItem("5"); box2.addItem("6"); box2.addItem("7"); box2.addItem("8"); box2.addItem("9"); box2.setBounds(620,100,50,30); c.add(box2); linfo9=new JLabel("M"); linfo9.setBounds(750,150,100,30); c.add(linfo9); box3=new JComboBox(); box3.addItem("0"); box3.addItem("1"); box3.addItem("2"); box3.addItem("3"); box3.addItem("4"); box3.addItem("5"); box3.addItem("6"); box3.addItem("7"); box3.addItem("8"); box3.addItem("9"); box3.setBounds(780,150,50,30); c.add(box3); linfo10=new JLabel("F"); linfo10.setBounds(870,150,80,30); c.add(linfo10); box4=new JComboBox(); box4.addItem("0"); box4.addItem("1"); box4.addItem("2"); box4.addItem("3"); box4.addItem("4"); box4.addItem("5"); box4.addItem("6"); box4.addItem("7"); box4.addItem("8"); box4.addItem("9"); box4.setBounds(900,150,50,30); c.add(box4); linfo11=new JLabel("C"); linfo11.setBounds(970,150,80,30); c.add(linfo11); box5=new JComboBox(); box5.addItem("0"); box5.addItem("1"); box5.addItem("2"); box5.addItem("3"); box5.addItem("4"); box5.addItem("5"); box5.addItem("6"); box5.addItem("7"); box5.addItem("8"); box5.addItem("9"); box5.setBounds(1000,150,50,30); c.add(box5); linfo12=new JLabel("NO OF ROOMS"); linfo12.setBounds(2,150,100,30); c.add(linfo12); box6=new JComboBox(); box6.addItem("0"); box6.addItem("1"); box6.addItem("2"); box6.addItem("3"); box6.addItem("4"); box6.setBounds(85,150,50,30); c.add(box6); linfo13=new JLabel("AC/NONAC"); linfo13.setBounds(512,150,100,30); c.add(linfo13); box7=new JComboBox(); box7.addItem("AC"); box7.addItem("NONAC"); box7.setBounds(600,150,80,30); c.add(box7); linfo14=new JLabel("ROOM NUMBER ALLOTED"); linfo14.setBounds(10,200,150,30); c.add(linfo14); box12=new JComboBox(); box12.addItem("...."); box12.addItem("1"); box12.addItem("2"); box12.addItem("3"); box12.addItem("4"); box12.addItem("5"); box12.addItem("6"); box12.addItem("7"); box12.addItem("8"); box12.addItem("9"); box12.setBounds(150,200,50,30); c.add(box12); linfo15=new JLabel("PURPOSE"); linfo15.setBounds(512,200,150,30); c.add(linfo15); box8=new JComboBox(); box8.addItem("SITE SEEING"); box8.addItem("Official"); box8.addItem("personal"); box8.addItem("OTHER"); box8.setBounds(600,200,80,30); c.add(box8); linfo16=new JLabel("EXPECTED DAYS"); linfo16.setBounds(900,200,150,30); c.add(linfo16); box9=new JComboBox(); box9.addItem("1"); box9.addItem("2"); box9.addItem("3"); box9.addItem("4"); box9.addItem("5"); box9.addItem("6"); box9.addItem("7"); box9.addItem("8"); box9.addItem("9"); box9.setBounds(1000,200,50,30); c.add(box9); linfo17=new JLabel("VEHICAL TYPE"); linfo17.setBounds(512,240,150,30); c.add(linfo17); box10=new JComboBox(); box10.addItem("TWO WHEELER"); box10.addItem("AUTO"); box10.addItem("OWN FOURWHEELER"); box10.addItem("OTHER"); box10.setBounds(610,240,100,30); c.add(box10); linfo18=new JLabel("VEHICAL NO"); linfo18.setBounds(900,240,100,30); c.add(linfo18); t7=new JTextField(10); t7.setBounds(1000,250,100,20); t7.setMaximumSize(t7.getPreferredSize()); c.add(t7); linfo19=new JLabel("OWNER/DRIVER NAME"); linfo19.setBounds(900,280,150,30); c.add(linfo19); t8=new JTextField(10); t8.setBounds(1055,290,100,30); t8.setMaximumSize(t8.getPreferredSize()); c.add(t8); linfo20=new JLabel("EXPENDITURE DETAILS"); linfo20.setBounds(512,300,150,30); c.add(linfo20); box11=new JComboBox(); box11.addItem("ADVANCES"); box11.addItem("DOBI"); box11.addItem("CURRENT BILL"); box11.addItem("TELEPHONE BILL"); box11.addItem("ROOM FRESHNERS"); box11.addItem("POOJA"); box11.addItem("DIESEL"); box11.addItem("CABEL BILL"); box11.addItem("REPAIRS"); box11.addItem("OTHERS"); box11.setBounds(700,300,100,30); c.add(box11); linfo21=new JLabel("AMOUNT"); linfo21.setBounds(1000,350,150,30); c.add(linfo21); t9=new JTextField(10); t9.setBounds(1000,370,100,30); t9.setMaximumSize(t9.getPreferredSize()); c.add(t9); bback=new JButton("BACK" ,new ImageIcon("IMAGESBUTTTONS/buttons_back.png")); bback.setBounds(230,515,120,30); add(bback); bsub=new JButton("",new ImageIcon("IMAGESBUTTTONS/submit.jpg")); bsub.setBounds(512,515,100,30); add(bsub); bexit=new JButton("" ,new ImageIcon("IMAGESBUTTTONS/btnCloseRed.gif")); bexit.setBounds(730,515,80,40); add(bexit); t4.addActionListener(this); t5.addActionListener(this); bsub.addActionListener(new sub()); bback.addActionListener(new back()); bexit.addActionListener(new exit()); } public void actionPerformed(ActionEvent ae) { } class back implements ActionListener { public void actionPerformed(ActionEvent ae) { } } class sub implements ActionListener { public void actionPerformed(ActionEvent ae) { //new JDBCNew(); new GVPdataInsert(); setVisible(false); } } class exit implements ActionListener { public void actionPerformed(ActionEvent ae) { System.exit(0); } }
}
**
*
import java.sql.*; import java.util.*; import java.awt.*; import javax.swing.*;
public class GVPdataInsert {
public Connection con = null; public PreparedStatement pstmt = null; public ResultSet rs = null; public GVPdataInsert() { try{ JB j=new JB(); Connection con=DBUtil.getConn(); System.out.print("connection getting"); pstmt=con.prepareStatement("insert into gvpdata values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); System.out.println("preapared Statement created"); pstmt.setString(1,j.date); pstmt.setInt(2,j.receipt_no); pstmt.setString(3,j.name); pstmt.setString(4,j.from_address); pstmt.setInt(5,j.contact_no); pstmt.setInt(6,j.no_of_persons); pstmt.setInt(7,j.males); pstmt.setInt(8,j.females); pstmt.setInt(9,j.child); pstmt.setInt(10,j.no_of_rooms); pstmt.setString(11,j.room_type); pstmt.setInt(12,j.allotedroom_no); pstmt.setDouble(13,j.amount); System.out.println("set values"); pstmt.executeUpdate(); System.out.println("excuted"); con.commit(); }catch(Exception e){ e.printStackTrace(); }
}
public static void main(String[] args) { System.out.println("Success"); }
}
//note: please send reply code for me...i have urgent requirement..
thnks & Regards, SUNIL
Blockquote
Here is a code that inserts form data to database using java swing.
import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; import java.sql.*; class Form extends JFrame { JButton ADD; JPanel panel; JLabel label1,label2,label3,label4,label5; final JTextField text1,text2,text3,text4,text5; Form(){ label1 = new JLabel(); label1.setText("UserID:"); text1 = new JTextField(20); label2 = new JLabel(); label2.setText("First Name:"); text2 = new JTextField(20); label3 = new JLabel(); label3.setText("Last Name:"); text3 = new JTextField(20); label4 = new JLabel(); label4.setText("ADDRESS:"); text4 = new JTextField(20); label5 = new JLabel(); label5.setText("Email:"); text5 = new JTextField(20); ADD=new JButton("ADD"); panel=new JPanel(new GridLayout(6,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(text4); panel.add(label5); panel.add(text5); panel.add(ADD); add(panel,BorderLayout.CENTER); setTitle("FORM"); ADD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); String value3=text3.getText(); String value4=text4.getText(); String value5=text5.getText(); try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(id,fname,lname,address,email) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"')"); JOptionPane.showMessageDialog(null,"Inserted Successfully!"); } catch(Exception e){} } }); } } class FormDemo { public static void main(String arg[]) { try { Form frame=new Form(); frame.setSize(300,300); frame.setVisible(true); } catch(Exception e){ } } }