I want retrieve the value picked from combo box to jtextfield. As per my UI combobox is in 4th. So I coded:
pst.setString(4, (String)cmbPaySub.getSelectedItem());
and the error pop-up:
Parameter index out of range.(4> number of parameters, which is 1".
I tried by coding;
pst.setString(1, (String)cmbPaySub.getSelectedItem());
Neither error will pop-up and value also not picking up.
private void cmbPaySubActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sipnena", "root", "");
String sql="select * from payments where cmbSubject=?";
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(4, (String)cmbPaySub.getSelectedItem());
ResultSet rs=pst.executeQuery();
while(rs.next()){
txtFee.setText(rs.getString("Fee"));
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Kindly help me to retrieve the value to the jtextfield.
When reading the result you're replacing the entire text by the result's value in a loop so only the last value will show:
while(rs.next()){
txtFee.setText(rs.getString("Fee"));
}
You should concatenate all the values, and then set the text:
StringBuilder text = new StringBuilder();
while(rs.next()){
text.append(rs.getString("Fee"));
text.append("\n");
}
txtFee.setText(text.toString());
Related
when press update button it shows:
"java.sql.SQLException:parameter out of range(1>number of parameters,which is 0)".
private void updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/smakdb","root","kisal400");
String sql="Update itemk set name=?,type=?, buying price=?, selling price=?,description=? where itemid=?";
pst=conn.prepareStatement(sql);
pst.setString(1, name2.getText());
String value=type2.getSelectedItem().toString();
pst.setString(2,value);
pst.setDouble(3,Double.parseDouble(buying2.getText()));
pst.setDouble(4,Double.parseDouble(selling2.getText()));
pst.setString(5,descript2.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "updated!!!");
conn.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
You have 6 question mark in the query, why not set the 6th parameter value?
You need set itemid as well.
In String sql you have six parameters "?" and once set five parameters.
I am facing a problem while completing my project.
I have created a JFrame with 2 JPanels. The first panel is the login panel, after the user enters his ID and password, he will be directed to the second panel to view his information (ID, Name, Medical Situation) which are saved in the databased connected to my Eclipse project.
The "Login" button already has a query using actionPerformed to verify if the password and ID are correct (if they exist in the database), so the user can login successfully.
What I want to do now is to create another query, to fetch (getText) ID, Name and Medical Situation from the database, depending on the ID the user will enter in JTextField = idTXT when he login, and setText in these JLabels: IDdraw_LBL, namedraw_LBL, msdraw_LBL in the second JPanel
Please check my code below:
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
//setting visibility of panels
patient_frame.setVisible(true);
patient_login.setVisible(false);
patient_appointments.setVisible(false);
//connecting to database
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/clinic", "root", "newpass");
//verifying ID and password code
String query="select * from patients where P_ID=? and Password=?";
PreparedStatement pst=con.prepareStatement(query);
pst.setString(1, idTXT.getText());
pst.setString(2, passwordField.getText());
ResultSet rs=pst.executeQuery();
int count =0;
while(rs.next()){
count=count+1;
}
if (count ==1){
JOptionPane.showMessageDialog(null, "Welcome!");
}
else{
JOptionPane.showMessageDialog(null, "Incorrect username or password.");
}
rs.close();
pst.close();
I tried to write another query to retrieve the information from database but it doesn't work:
idTXT.getText();
String query1="select * from patients where P_ID=?";
PreparedStatement pst1=con.prepareStatement(query1);
ResultSet rs1=pst1.executeQuery();
while (rs1.next()){
IDdraw_LBL.setText(rs.getString("P_ID"));
namedraw_LBL.setText(rs.getString("Name"));
msdraw_LBL.setText(rs.getString("Medical_Situation"));
}
pst1.close();
}catch(Exception exx) {
JOptionPane.showMessageDialog(null, e);
}
}
}
It gives me this long-window error when running the program
Any help will be appreciated, Thanks in advance!
You're missing a parameter to your query:
String query1="select * from patients where P_ID=?";
PreparedStatement pst1=con.prepareStatement(query1);
ResultSet rs1=pst1.executeQuery();
You've defined an input parameter (?), but you're not assigning it before executing the query.
For example:
pst1.setInt(1, MY_ID);
ResultSet rs1=pst1.executeQuery();
I figured out that i didn't write the ResultSet parameter correctly in the while loop. It is rs1 not rs:
IDdraw_LBL.setText(rs1.getString("P_ID"));
Thank you !!
I created a sequence in Oracle 10g because I have to increase case_number field auto_incremently. But I'm getting an error.
private void button_saveandsubmitActionPerformed(java.awt.event.ActionEvent evt) {
con = JavaConnectDB.ConnectDB();
try{
String sql="insert into FIR_form values(case_number_sequence,?,?,?,?,?,?,?,?,?,?,?)";
pst = (OraclePreparedStatement) con.prepareStatement(sql);
pst.setString(1,text_date.getText());
pst.setString(2,text_district.getText());
pst.setString(3,text_subject.getText());
pst.setString(4,text_description.getText());
pst.setString(5,text_cfullname.getText());
pst.setString(6,text_fhname.getText());
pst.setString(7,text_caddress.getText());
pst.setString(8,text_contact.getText());
pst.setString(9,text_suspectfullname.getText());
pst.setString(10,text_suspectaddress.getText());
pst.setString(11,text_suspectdescription.getText());
rs = (OracleResultSet) pst.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "the FIR has been added successfully!!!");
con.close();
this.dispose();
}
else{
JOptionPane.showMessageDialog(null, "enter all fields appropriately first!!!");
}
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null, "An error occured. try again later !!!");
}
}
The field in the table will not auto-populate because you have defined a sequence. You must either reference the sequence.nextval in your insert statement to insert the value, or add a trigger to the table to populate the column from the sequence.
See this post for an example:
i have the following jframe :
and i wanna make the buttons work im still new to programming can someone help me please? i want the add row btn to add a new row to database, the update btn let me save changes and delete delete the selected row, also the jTextBoxes are connected to the database
i tried doing this to update :
Connection conn=null;
PreparedStatement pst = null;
try{
String value1=txt_cid.getText();
String value2=txt_carid.getText();
String value3=txt_aid.getText();
String value4=txt_rd.getText();
String value5=txt_bd.getText();
String value6=txt_bn.getText();
String sql="update booking set customer_id'"+value1+"',car_id'"+value2+"',agency_id'"+value3+"',return_date'"+value4+"',booking_date'"+value5+"',booking_number'"+value6+"',";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "table updated");
}catch(Exception e) {
JOptionPane.showMessageDialog(null,e);
}
but it didnt work out for me i get exception error
You haven't stated what the error is but UPDATE takes an equals operator for every parameter. Also use PreparedStatement placeholders to avoid SQL Injection attacks:
String sql = "update booking set customer_id=?, car_id=?,agency_id=?,return_date=?,booking_date=?,booking_number=?";
pst = conn.prepareStatement(sql);
pst.setInt(1, value1);
pst.setInt(2, value2);
... // set the other parameters
Read: UPDATE Syntax
I have a textfield in a NetBeans frameform. I added a "Generate" button, and when I click it the program should find the largest value from the column "book_code" in a table named "books" and should display "generated value+1" in the textfield.
I use a class method to connect to my database so you won't find any code for connecting the database.
I have tried some coding but I am not able to generate the maximum value from book_code column of table books. Here is what I did:
String b_code="select max(Book_code) from books";
try
{
pst = conn.prepareStatement(b_code);
rs=pst.executeQuery();
int a=rs.getInt(b_code);
System.out.println("Book code is "+a);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null , e);
}
The maximum value in book_code column is 103, and when I click the "generate" button I would like to see 104 in the textfield.
There are at least two (2) issues with your code:
You create your ResultSet but you don't call rs.next() before you try to retrieve the value.
When you do try to retrieve the value you use the variable that contains the SQL string, and there is no column in the ResultSet that matches
This should work better:
String b_code="select max(Book_code) from books";
try {
pst = conn.prepareStatement(b_code);
rs = pst.executeQuery();
rs.next();
int a = rs.getInt(1);
System.out.println(String.format("max(Book_code) is %d", a));
} catch(Exception e) {
JOptionPane.showMessageDialog(null , e);
}