Mysql update query through Netbeans Jform - mysql

I am trying to run below code on netbeans but it is throwing this error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, esalary=?, eage=?, egender=?, edept=? where eid = ?' at line 1".
please help me with the error
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
//open connection
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","BakerStreet#221b");
//mysql query to update
String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
PreparedStatement ptsmt = con.prepareStatement(sql);
ptsmt.executeUpdate(sql);
ptsmt.setString(1,empName.getText());
ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
ptsmt.setString(4,empGen.getText());
ptsmt.setString(5,empDep.getText());
ptsmt.setInt(6,Integer.parseInt(id.getText()));
ptsmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Record updated Successfully");
con.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}

you have called twice executeUpdate(sql) so remove one
you have called executeUpdate(sql) before adding value to parameters so do this :
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
//open connection
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","BakerStreet#221b");
//mysql query to update
String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
PreparedStatement ptsmt = con.prepareStatement(sql);
ptsmt.setString(1,empName.getText());
ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
ptsmt.setString(4,empGen.getText());
ptsmt.setString(5,empDep.getText());
ptsmt.setInt(6,Integer.parseInt(id.getText()));
//execute update ...
ptsmt.executeUpdate();
JOptionPane.showMessageDialog(this, "Record updated Successfully");
con.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}

Related

incompatible types: java.sql.Statement cannot be converted to java.beans.Statement

I am trying to make a ATM Project in Net beans but I am stack here.
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Statement st = null;
if (accNumField.getText().isEmpty() || pinField.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Please fill in the Fields");
}else{
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bankmanagement", "root", "");
String query = "Select from * clients where AccNum='"+accNumField.getText()+"' and PIN="+pinField.getText()+"";
**st = con.createStatement();
rs= st.executeQuery(query); **
if(rs.next()){
new MainMenu().setVisible(true);
this.dispose();
}else{
}
} catch (Exception e) {
enter image description hereCan someone help me where is the problem in my code?
I am trying to get access in the database and authentication the user in my bank.
Thanks for your time

why is my database not updating? (using netbeans xampp mysql)

when i run the file, it accepts the query and says update success but when i check my database why does it not update?
private void btnUpdateDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(radioUpdate.isSelected()){
try{
Class.forName("com.mysql.jdbc.Driver");
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/soften?zeroDateTimeBehavior=convertToNull","root","");
st=cn.createStatement();
String sql="UPDATE `tblproductssales` SET "
+" product = ?,quantity = ?"
+" WHERE 'sale no' = ?";
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1,editProduct.getText());
pst.setString(2,editQty.getText());
pst.setString(3,editSaleID.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"success update");
//**when i run the file this JOPtionpPane shows
}catch(Exception e){e.printStackTrace();}
}
}
here is my database, the table name is tblproductssales

Execute Multiple JDBC Queries In a Defined Sequence

I'm develop an application to connect MySQL via JDBC. In an action, I need to execute two queries, sql to read, and sql to update.
To make sure, sql to read query would be executed first, and sql to update to be executed later, I use JDBC transaction. But somehow, the problem is, mysql execute 2nd query first, and then the first read query.
Looking for suggestions. Many thanks.
// Sql connection
Connection conn = null;
Statement stmt = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementUpdate = null;
String sql = "SELECT item_name, item_detail FROM Order_Printing where kitchen_id = ? and order_printed = ?";
String sqlFlag = "UPDATE order_printing set order_printed = 1 where kitchen_id = ?";
try {
// Register JDBC driver (Note to add mysql connector jar file)
Class.forName("com.mysql.jdbc.Driver");
// Step 3: open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
conn.setAutoCommit(false); // Disable auto-commit mode
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 4);
preparedStatement.setInt(2, 0);
ResultSet rs = preparedStatement.executeQuery();
//
preparedStatementUpdate = conn.prepareStatement(sqlFlag);
preparedStatementUpdate.setInt(1, 4);
preparedStatementUpdate.executeUpdate();
int startingPos = 10;
int orderNumber = 1;
while (rs.next()) {
String item_name = "Order " + orderNumber++ + ": ";
item_name += rs.getString("item_name");
String item_detail = rs.getString("item_detail");
startingPos += 20;
g.drawString(item_name, 0, startingPos);
startingPos += 20;
g.drawString(item_detail, 0, startingPos);
}
conn.commit();
} catch (SQLException se) {
se.printStackTrace();
} catch (ClassNotFoundException se) {
se.printStackTrace();
} catch (Exception se) {
se.printStackTrace();
} finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
Try executing rs.next() while loop BEFORE you call preparedStatementUpdate.executeUpdate();

Database doesn't work on netbeans

My database does not work, I get and error that I pasted after the code:
try {
String url = "jdbc:derby:test";
String name = "root";
String password = "123";
Connection con = DriverManager.getConnection(url, name, password);
Statement state = con.createStatement();
String query = "INSERT INTO APP.UNTITLED (id , name , password ) VALUES (1, 'Anas','123456789')";
state.execute(query);
}
catch (SQLException e){
e.printStackTrace();
}
}
}
this is the error that appears:
java.sql.SQLException: No suitable driver found for jdbc:derby:test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at testdatabase.TestDatabase.main(TestDatabase.java:25)
BUILD SUCCESSFUL (total time: 0 seconds)
You must use Class.forName to look for the classpath to find suitable driver.
try {
...
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection con = DriverManager.getConnection(url, name, password);
Statement state = con.createStatement();
...
}
catch (....
Don't forget to add library to your project
java DB Driver
with following jar files.
derby.jar
derbyclient.jar
derbynet.jar

jdbc to MYSQL error: "table airportdetails doesn't exist"

I am trying to connect to a MySQL database from a jsp page using jdbc in the backend.
I have the following code:
public static void insertIntoDatabase(String code,String name,String temp,String hum,String del) {
Connection con = null;
if (del.length() == 0) {
del="no data";
}
name = name.replaceAll("\\(.+?\\)", "");
name = name.replaceAll(" ", "_");
del = del.replaceAll(" ", "_");
System.out.println("del "+del);
String url = "jdbc:mysql://localhost:3306/test";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url,"root","");
con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS aiportdetails(code VARCHAR(50) PRIMARY KEY, " +
"name VARCHAR(250), temp VARCHAR(50), hum VARCHAR(50), del VARCHAR(50))");
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
I am getting the following error at
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
error:
Table 'test.airportdetails' doesn't exist
But from my phpmyadmin I can see that the table is created and exists:
What is the reason I am getting this error?
Thank you.
executeUpdate()
Executes the SQL statement in this PreparedStatement object, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.
Currently you are trying to use this for creating a table. That's the reason why you are getting that error.
Refer to the documentation Java 6 OR Java 1.4.2 for executeUpdate
EDIT:
You should create a table using Statement
Statement st = con.createStatement();
String table = "Create table .......";
st.executeUpdate(table);
you can put the initialize the connection and load driver at the constructor level, then in the method you can first createt check the table if it exists or create it then if it is successful, continue with the insert operation.like this:
public class MyBean{
String url = "jdbc:mysql://localhost:3306/test,"root","" ";
public MyBean(){
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url);
}catch(Exception e){
}
}
public static void insertIntoDatabase(String code,String name,String temp,String hum,String del) {
Connection con = null;
if (del.length() == 0) {
del="no data";
}
name = name.replaceAll("\\(.+?\\)", "");
name = name.replaceAll(" ", "_");
del = del.replaceAll(" ", "_");
System.out.println("del "+del);
try {
con = DriverManager.getConnection(url);
Int result = con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS aiportdetails(code VARCHAR(50) PRIMARY KEY, " +
"name VARCHAR(250), temp VARCHAR(50), hum VARCHAR(50), del VARCHAR(50))");
if(result>0){
try{
ResultSet rs = con.prepareStatement("SELECT * FROM airportdetails;").executeQuery();
}catch(Exception e){
}finally{
}
}//end if
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}