Issue
I'm trying to insert data to data base by my own interface using java netbean But I'm stuck with insert query doesn't work here is my code segment
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package youthsociety;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author Rathnayaka RMBS
*/
public class dbop {
String url="jdbc:mysql://localhost:3306/youthsociety";
String username="root";
String password="";
Connection con=null;
Statement st=null;
public void addmember(memberdata m){
try{
con=(Connection)DriverManager.getConnection(url,username,password);
String query="INSERT INTO members VALUES(?,?,?,?,?,?,?,?)";
st=(Statement)con.createStatement();
st.executeUpdate(query);
}catch(Exception e){
}
}
}
st.executeUpdate(query) mark as wrong.
Solution
Ok, let me explain your problem.
- You don’t have configured jdbc driver.
- Don’t need to cast anything!
- You have question mark ‘?’ In your query, and you are using simple Statement class to execute instead of PreparedStatement.
Now, if you are using simple Statement, follow this link: A Java MySQL INSERT example (using Statement)
If you are using PreparedStatement, you can follow this link: A Java MySQL INSERT example (using PreparedStatement)
Answered By - Salman Sabir
Answer Checked By - Marie Seifert (JavaFixing Admin)