How to update mysql database value from java netbeans
I need to update one of my selected column value
this table has 6 column but I need to update one column only (6th column)
So how to do this -
I use this code -
connectDB();
try{
String value1 = txt_searchcode.getText();
String value6 = Final_stock_view.getText();
String sql = "Update tbl_codes set No='"+value1+"',Bin_Balance='"+value6+"'WHERE No='"+value1+"' ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.execute();
JOptionPane.showMessageDialog(null, " Your new records Updated Succsessfully!!");
}catch(Exception e){
JOptionPane.showMessageDialog(null," Update Failed");
}
closeDB();
its running well without any errors, but nothing happen
So how to fix this...... (Examples)
Thanks
My insert into is giving me issues.
Im pretty sure the Jtextfield is the problem.
If anyone can give me a nudge in the right direction I would greatly appreciate it.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:#//localhost:1521/xe";
Connection conn = DriverManager.getConnection(url, "system", "oracle");
conn.setAutoCommit(false);
String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";
Statement stm = conn.createStatement();
stm.execute(query1);
JOptionPane.showMessageDialog(null, "Record Added Succesfully.", "Record Added",
JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
System.out.println(e);
}
}
nameTextField.getSelectedText()
I'm guessing that should be
nameTextField.getText()
Also, use a PreparedStatement for the SQL. You will less likely to make SQL syntax errors. Check out: geting data from multiple table when selecting a row then clicking a button for a basic example.
Im pretty sure the Jtextfield is the problem.
Well, learn how to do some basic debugging instead of guessing.
String query1 = "INSERT INTO WIN(NAME) VALUES('"+nameTextField.getSelectedText()+"')";
The above code can be written like:
String name = textField.getSelectedText();
// now you can verify the value of the name
System.out.println(name);
String query1 = "INSERT INTO WIN(NAME) VALUES('"+ name +"')";
// verify SQL syntax
System.out.println( query1);
Of course my first suggestion to use the PreparedStatement is easier, but learn how to display the value of variables when you do debugging.
I am having a difficult time with just updating the data within my SQLite Database. I have tried using a BinaryStream however no luck. So I then decided, screw it. No need to update the Blob, if I delete any code related to the BLOB (even in the SQL Statement) the Update statement doesn't execute (properly). Prior to adding the BLOB everything worked fine. Now I just can't seem to understand why I cannot update my database any longer. If I take out the BLOB, program will say "Employee has successfully added to the database" however, when I look at my database, all information is the same. I can insert, search and delete them methods are fine, just updating just doesn't seem to be working for me. I am running out of ideas on what to do, could someone possibly help me? Even if it's not updating the BLOB, at this rate I'll take anything. Update or no update.
Code -
updateEmployee.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Connection connection = null;
PreparedStatement pst = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
connection.setAutoCommit(false);
String sql = "UPDATE employees SET ID =?, Name=?, Gender=?, DOB=?, Address=?, Postcode=?, NIN=?, JobTitle=?, StartDate=?, Salary=?, Email=?, Images=? WHERE ID=?";
pst = connection.prepareStatement(sql);
pst.setInt(1,Integer.parseInt(idTextField.getText()));
pst.setString(2, nameTextField.getText());
pst.setString(3, genderTextField.getText());
pst.setString(4, dobTextField.getText());
pst.setString(5, addressTextField.getText());
pst.setString(6, postcodeTextField.getText());
pst.setString(7, ninTextField.getText());
pst.setString(8, jobtitleTextField.getText());
pst.setString(9, startdateTextField.getText());
pst.setString(10, salaryTextField.getText());
pst.setString(11, emailTextField.getText());
pst.setBytes(12, readFile(s));
pst.executeUpdate();
System.out.println("EmployeeAdded");
JOptionPane.showMessageDialog(null, "Employee has successfully added to the database");
connection.commit();
pst.close();
connection.close();
}
catch ( Exception e1 ) {
JOptionPane.showMessageDialog(null, "Uh oh! Something went wrong!");
}
}
});
Thank you.
Sorted the issue.
It was with the line-
String sql = "UPDATE employees SET ID =?, Name=?, Gender=?, DOB=?, Address=?, Postcode=?, NIN=?, JobTitle=?, StartDate=?, Salary=?, Email=?, Images=? WHERE ID=?";
I needed to add pst.setInt(12,Integer.parseInt(idTextField.getText())); for WHERE ID=?.
Never happened to me before though, alas I'll take it.
Your command text has 13 parameter placeholders (?) but you are only defining 12 parameters. You are missing the last one, which should presumably be the same value as the first one ("ID").
private void checkKeyPressed(java.awt.event.KeyEvent evt) {
try{
String ch ="select * from check where name like ?%";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText());
ResultSet rs=pst.executeQuery();
checker.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
above is the code im using, and im getting a:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near "check": syntax error)
i have a table named check in sqlite, and im trying to create a search engine in my jframe, so when i enter text in a textfield the jtable filters automatically. any idea why this error could come about.
You have to change this part, if you need to pass % you have to pass it with set Method and as check is a reserved keyword in sqlite change it to check
String ch ="select * from check where name like ?%";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText());
with
String ch ="select * from `check` where name like ?";
PreparedStatement pst=conn.prepareStatement(ch);
pst.setString(1, check.getText()+"%");
I want to fill the jComboBox with values from database. and these values depend on the text written in the textfield.
eg: if I write a in the texfield, the combobox will have all values starting with a. The values are from a databse
Here's my code:
private void FillCombo(){
String url = "jdbc:mysql://localhost:3306/pharmacy";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "test";
String sql = "select medicinename from medicine where medicinename like '%"+jTextField5.getText()+"%'";
try{
Class.forName(driver).newInstance();
System.out.println("1");
Connection con = (Connection)DriverManager.getConnection(url,user,pass);
System.out.println("Connected");
Statement st=(Statement) con.createStatement();
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()){
String name = rs.getString("medicinename");
jComboBox1.addItem(name);
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}}
I want not just one caracter eg: if i write a, as, asp it fill aspirine myproblem is that the combobox is already filled. want it empty when i start.
Simply use JComboBox#removeAllItems() to removes all items from the item list on each stroke of any key in the JTextField.
Points to Remember
Don't load driver each time you enter a new character. Move it outside the FillCombo() method.
Use PreparedStatement instead of using single quoted query string that may cause issue. Find a sample on Using Prepared Statements
Don't forget to close the resources such as connection, result set and statement.
Use finally block to handle it or Read more about Java7 -The try-with-resources Statement