Insert data in database through java swings.
String brandname=jTextField1.getText();
int no=Integer.parseInt(jTextField2.getText());
String vendorname=jTextField3.getText();
String des=jTextArea1.getText();
int quan=Integer.parseInt(jTextField4.getText());
String productcategory=jTextField10.getText();
String size1=jTextField5.getText();
String col=jTextField36.getText();
String pdate = ((JTextField)jd4.getDateEditor().getUiComponent()).getText();
String sellingprice=jTextField7.getText();
String pcost=jTextField8.getText();
String pmodel=jTextField9.getText();
String discount=(String) jComboBox2.getSelectedItem();
//String dis=jTextField11.getText();
if(size1.equals(""))
{
try
{
Connection c=new Connection();
java.sql.Connection con=c.connectit();
PreparedStatement pst1=con.prepareStatement("select * from stock where id='"+no+"'");
ResultSet rs=pst1.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Already Exists!!!!");
}
else
{
PreparedStatement pst3;
pst3 = con.prepareStatement("insert into stock values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
pst3.setInt(1, no);
pst3.setString(2, brandname);
pst3.setString(3,vendorname);
pst3.setString(4,des);
pst3.setInt(5,quan);
pst3.setString(6,productcategory);
pst3.setNull(7,Types.INTEGER);
pst3.setString(8,col);
pst3.setString(9,pdate);
pst3.setString(10,sellingprice);
pst3.setString(11,pcost);
pst3.setString(12,pmodel);
pst3.setString(13,discount);
//pst.setString(13,dis);
int i1=pst3.executeUpdate();
if(i1>0)
{
JOptionPane.showMessageDialog(null,"Insert");
con.close();
pst3.close();
pst1.close();
settext();
}
else
{
JOptionPane.showMessageDialog(null,"Error");
con.close();
pst3.close();
pst1.close();
settext();
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
System.out.println(e);
}
}
else
{
int size=Integer.parseInt(jTextField5.getText());
try
{
Connection c=new Connection();
java.sql.Connection con=c.connectit();
PreparedStatement pst11=con.prepareStatement("select * from stock where id='"+no+"'");
ResultSet rs=pst11.executeQuery();
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Already Exists!!!!");
}
else
{
PreparedStatement pst=con.prepareStatement("insert into stock values(?,?,?,?,?,?,?,?,?,?,?,?,?)");
pst.setInt(1, no);
pst.setString(2, brandname);
pst.setString(3,vendorname);
pst.setString(4,des);
pst.setInt(5,quan);
pst.setString(6,productcategory);
pst.setInt(7,size);
pst.setString(8,col);
pst.setString(9,pdate);
pst.setString(10,sellingprice);
pst.setString(11,pcost);
pst.setString(12,pmodel);
pst.setString(13,discount);
//pst.setString(13,dis);
int i=pst.executeUpdate();
if(i>0)
{
JOptionPane.showMessageDialog(null,"Insert");
con.close();
pst.close();
settext();
}
else
{
JOptionPane.showMessageDialog(null,"Error");
con.close();
pst.close();
settext();
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
System.out.println(e);
}
}
table();
}