I create a jForm in Netbeans which has 8 columns. I want to insert values of 2 to 7 fields at run time and the 1st field value should be auto incremented through the database. I create a table in ms access, where the first field has auto number datatype.
when i write the insert query, if i take 7 question marks , and pass values from 2 , it shows the error of " database fields values are not same". How to do this, that frst field is autoincremented and next fields will insert the values.. Here is my code...
Connection myconnection= null;
String mydriver= "sun.jdbc.odbc.JdbcOdbcDriver";
if((jTextField2.getText().length()==0)||(jTextArea1.getText().length()==0)||(jRadioButton1.getText().length()==0)||(jRadioButton2.getText().length()==0)||(jFormattedTextField1.getText().length()==0)||(jTextField3.getText().length()==0)||(jComboBox1.getSelectedIndex()==0)||(jFormattedTextField3.getText().length()==0)) {
JOptionPane.showMessageDialog(rootPane, "Please fill all the details");
}
else
{
try
{
Class.forName(mydriver);
myconnection=DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq= C:\\Documents and Settings\\Administrator\\My Documents\\NetBeansProjects\\DAV REHABILITATION\\Davdb.accdb; Uid=Admin; Pwd=;");
try
{
String myquery= "insert into Patients values(?,?,?,?,?,?,?)";
PreparedStatement mystatement= myconnection.prepareStatement(myquery);
mystatement.setString(2,jTextField2.getText());
mystatement.setString(3,jTextArea1.getText());
if(jRadioButton1.isSelected()) {
mystatement.setString(4, "Male");
} else if(jRadioButton2.isSelected()) {
mystatement.setString(4, "Female");
}
mystatement.setString(5, jFormattedTextField1.getText());
mystatement.setString(6, jTextField3.getText());
mystatement.setString(7, jComboBox1.getSelectedItem().toString());
mystatement.setString(8, jFormattedTextField3.getText());
mystatement.executeUpdate();
JOptionPane.showMessageDialog(rootPane,"Data Saved");
mainform obj=new mainform();
this.setVisible(false);
obj.setVisible(true);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(rootPane, "Error in Query. " + e.getMessage());
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(rootPane, " Error Occured.Connection wrong" + e.getMessage());
}
}
}