I'm having problems in my program. Well, here's my code:
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String query = "INSERT INTO GuestList (id, Title, FirstName, MiddleName, LastName, Address, ContactNo, Occupation) values (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pst = conn.prepareStatement(query);
count++;
pst.setString(1, String.valueOf(textField.setText(count + "")); //<<--This is my problem
pst.setString(2, comboBox.getToolTipText());
pst.setString(3, fNameTextField.getText());
pst.setString(4, mNameTextField.getText());
pst.setString(5, lNameTextField.getText());
pst.setString(6, addressTextPane.getText());
pst.setString(7, contactNoTextField.getText());
pst.setString(8, occupationTextField.getText());
pst.execute();
JOptionPane.showMessageDialog(null, "Data Saved!");
pst.close();
}catch(Exception e){
}
}
});
I'm having an auto increment in the line that has a comment in it. It says that " - Syntax error, insert ")" to complete Expression" and "The method valueOf(Object) in the type String is not applicable for the arguments (void)"
What should I do to get rid of these? Can someone help me? Thank you. :)
for every opening group like this ( you will need a closing one like that )
you have here 3 open and 2 closed....
pst.setString(1, String.valueOf(textField.setText(count + ""));
so you are missing one )
try:
pst.setString(1, String.valueOf(textField.setText(count + "")));
Related
Im trying to create an application that allows users to create a profile however when im inserting into the DB I get the error shown above. I've had a look at similiar solutions but nothing seems to have worked.
The relevant code as it stands is;
//Invokes myConnection class to link to DB
Connection con = myConnection.getConnection();
PreparedStatement ps;
try
{
//Adds the selected text to DB
ps = con.prepareStatement("INSERT INTO `user`(`username`, `realname`, `password`, `email`, `gym`, `belt`, `dateofbirth`, `profilepic`, `biography`, `motto`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, jTextFieldUsername.getText());
ps.setString(2, jTextFieldName.getText());
ps.setString(3, String.valueOf(jPasswordFieldPass.getPassword()));
ps.setString(4, jTextFieldEmail.getText());
ps.setString(5, jTextFieldGym.getText());
ps.setString(6, jComboBoxBelt.toString());
ps.setDate(7, convertUtilDateToSqlDate(jDateChooserDOB.getDate()));
InputStream img = new FileInputStream(new File(imagePath));
ps.setBlob(8, img);
if(ps.executeUpdate() != 0)
{
JOptionPane.showMessageDialog(null, "Account Created!");
}
else
{
JOptionPane.showMessageDialog(null, "Oops! Something went wrong!");
}
ps.setString(9, jTextAreaBiography.getText());
ps.setString(10, jTextAreaMotto.getText());
}
catch (Exception ex)
{
Logger.getLogger(RegisterPage.class.getName()).log(Level.SEVERE, null, ex);
}
Sorry if this is straight forward and thanks in advance for your help!
Edit: Answered simply, thanks was having a complete brainfart there.
The problem in your code is,
You have to set all the values for the parameters and then use execute statement.
your code should be like this.
ps = con.prepareStatement("INSERT INTO `user`(`username`, `realname`, `password`, `email`, `gym`, `belt`, `dateofbirth`, `profilepic`, `biography`, `motto`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
ps.setString(1, jTextFieldUsername.getText());
ps.setString(2, jTextFieldName.getText());
ps.setString(3, String.valueOf(jPasswordFieldPass.getPassword()));
ps.setString(4, jTextFieldEmail.getText());
ps.setString(5, jTextFieldGym.getText());
ps.setString(6, jComboBoxBelt.toString());
ps.setDate(7, convertUtilDateToSqlDate(jDateChooserDOB.getDate()));
InputStream img = new FileInputStream(new File(imagePath));
ps.setBlob(8, img);
ps.setString(9, jTextAreaBiography.getText());
ps.setString(10, jTextAreaMotto.getText());
if(ps.executeUpdate() != 0)
{
JOptionPane.showMessageDialog(null, "Account Created!");
}
else
{
JOptionPane.showMessageDialog(null, "Oops! Something went wrong!");
}
You are running ps.executeUpdate() without setting parameters 9 and 10.
Move these lines before if(ps.executeUpdate() != 0):-
ps.setString(9, jTextAreaBiography.getText());
ps.setString(10, jTextAreaMotto.getText());
Ok, I want to preface. I have searched through multiple people having this issue and none of that seems to be my problem.
String Statement = "INSERT INTO DAISYEMPLOYEE"
+ "(SSN, FNAME, MINIT, LNAME, ADDRESS, DATEHIRED, PAY, PHONE#) VALUES "
+ "( ?, ?, ?, ?, ?, ?, ?, ?)";
JOptionPane.showMessageDialog(frame, Statement, "Daisy Imports", 3);
try {
PreparedStatement PST = connection.prepareStatement(Statement);
PST.setInt(1, 11);
PST.setString(2, "NAME");
PST.setString(3, "a");
PST.setString(4, "asdasd");
PST.setString(5, "123 hobo street");
PST.setString(6, "2017-05-06 ");
PST.setString(7, "8.5");
PST.setString(8, "1231231234");
PST.execute();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frame, "PUERILE CAPTION REMOVED", "Daisy Imports", 3);
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
} // try
I totally have parameters, I have no idea whats happening.
I want to insert primary key of selected item of combo box
but I got this error Parameter index out of range(12 > number of parameter which is 0)".
I really don't know how to fix this.
Here is my code:
String valueTrainer = "kosong";
try
{
String sql2 = "Insert into ahli (MemberID, TrainerID, Name, ICNumber, Address, Nationality,"
+ "PhoneNumber, Email, EmergencyPerson, EmergencyContact, DateReg, MemberTypeID) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql2);
pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());
//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);
pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());
//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser.getDateEditor().getUiComponent()).getText());
//membertype combobox
// problem start from here, I think..
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID from jeniskeahlian where Type = '"+memberTypeList+"' " ;
pst = conn.prepareStatement(sql1);
rs = pst.executeQuery();
while(rs.next()){
String memberType = rs.getString("MemberTypeID");
pst.setString(12, memberType);
}
pst.execute();
JOptionPane.showMessageDialog(null, "New member has been added");
} catch (SQLException | HeadlessException e) {
JOptionPane.showMessageDialog(null, e);
}
You are not using pst properly.
same named reference on prepared statement can not be used on different sql statement unless one is used and closed properly. Prepare, set, execute, close on one statement, and then repeat the same on another statement.
Change your code as below.
try
{
String memberTypeList = MemberTypeComboBox.getSelectedItem().toString();
String sql1 ="Select MemberTypeID
from jeniskeahlian
where Type = ? " ;
pst = conn.prepareStatement(sql1);
pst.setString( 1, memberTypeList );
rs = pst.executeQuery();
String memberType = "";
while(rs.next()){
memberType = rs.getString("MemberTypeID");
}
rs.close();
pst.close();
String sql2 = "Insert into ahli (MemberID, TrainerID, Name,
ICNumber, Address, Nationality,
PhoneNumber, Email, EmergencyPerson,
EmergencyContact, DateReg, MemberTypeID)
values(?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql2);
pst.setString(1, MemberIDTextField.getText());
pst.setString(2, valueTrainer);
pst.setString(3, NameTextField.getText());
pst.setString(4, jTextField1.getText());
pst.setString(5, AddressTextArea.getText());
//Nationality combo box
String nationalityList = NationalityComboBox.getSelectedItem().toString();
pst.setString(6, nationalityList);
pst.setString(7, PhoneNumberTextField.getText());
pst.setString(8, EmailTextField.getText());
pst.setString(9, EmerContactPersonTextField.getText());
pst.setString(10, EmerContactNumberTextField.getText());
//Date Chooser
pst.setString(11, ((JTextField)MemberDateChooser
.getDateEditor()
.getUiComponent()).getText());
pst.setString(12, memberType);
pst.execute();
JOptionPane.showMessageDialog(null, "New member has been added");
} // try
While your insert statement seems properly populated, in the while-loop you rare trying to execute a select statement which has no parameters, so that pst.setString(12,memberType) fails.
String sql1 ="Select MemberTypeID from jeniskeahlian where Type = '"+memberTypeList+"' " ; //no ? in this query
Before you execute the query you should have set all the values for all the parameters.
String Sql = " INSERT INTO asset_update
(asset_id,
date_acquired,
item_code,
serial_number,
cost_of_acquisition,
estimated_useful_life,
estimated_residual_value,
depreciation_start_date,
USER,
status,
disposal_date,
asset_image)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
pst = conn.prepareStatement(Sql);
pst.setString(1, assetid.getText());
pst.setString(2, ((JTextField) date_aquired.getDateEditor().getUiComponent()).getText());
String itemCode = ComboBox_itemCode.getSelectedItem().toString();
pst.setString(3, itemCode);
pst.setString(4, txt_sn.getText());
pst.setString(5, txt_coa.getText());
pst.setString(6, eul.getText());
pst.setString(7, txt_erv.getText());
pst.setString(8, ((JTextField) date_dsd.getDateEditor().getUiComponent()).getText());
String user = jComboBox_Users.getSelectedItem().toString();
pst.setString(9, user);
String status = ComboBox_status.getSelectedItem().toString();
pst.setString(10, status);
pst.setString(11, ((JTextField) date_dd.getDateEditor().getUiComponent()).getText());
pst.setBytes(12,persons_image);
this my code but what if i want to add another table with different columns to my insert statement
How can I insert records into two different table? I made two sql statement and it won't work.
private void cmdsubmitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String sql = "Insert into customer (Customer_ID,First_Name,Last_Name,Birthdate,Gender,Occupation,Address,Email,Contact,Status,Income,Amount,Term,Interest,Date_Applied,Purpose,Other) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, txtID.getText());
pst.setString(2, txtfname.getText());
pst.setString(3, txtlname.getText());
pst.setString(4,((JTextField)birthdate.getDateEditor().getUiComponent()).getText());
String gender = cmbgender.getSelectedItem().toString();
pst.setString(5, gender);
pst.setString(6, txtoccupation.getText());
pst.setString(7, txtaddress.getText());
pst.setString(8, txtemailadd.getText());
pst.setString(9, txtcontact.getText());
String status = cmbstatus.getSelectedItem().toString();
pst.setString(10, status);
String income = cmbincome.getSelectedItem().toString();
pst.setString(11,income);
pst.setString(12, txtamount.getText());
String period = cmbperiod.getSelectedItem().toString();
pst.setString(13, txtloan.getText() + " " + period);
pst.setString(14, txtinterest.getText());
pst.setString(15, ((JTextField)datechooser.getDateEditor().getUiComponent()).getText());
String purpose = cmbpurpose.getSelectedItem().toString();
pst.setString(16, purpose);
pst.setString(17, txtother.getText());
Double balance = Double.parseDouble(txtamount.getText()) * Double.parseDouble(txtinterest.getText());
String s = "Insert into payments (Customer_ID,Customer_Name,Amount,Interest,To_Pay) values (?,?,?,?,?)";
pst = conn.prepareStatement(s);
pst.setString(1, txtID.getText());
pst.setString(2, txtfname.getText()+","+txtlname.getText());
pst.setString(3, txtamount.getText());
pst.setString(4, txtinterest.getText());
pst.setString(5, balance.toString());
pst.execute();
JOptionPane.showMessageDialog(null, "Record Save");
Menu menu = new Menu();
this.setVisible(false);
menu.setVisible(true);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
The problem appears to be that you aren't calling pst.execute() before you call this line:
pst = conn.prepareStatement(s);
This is overwriting your previous pst variable; creating a new PreparedStatement.
I'm not entirely sure about that API, but it seems like you have one execute for two statements. Also, you should consider making a function if a good chunk of the code is exactly the same. Less places to maintain