Skip to content
Advertisement

java netbean inserting data to db does not work

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.

Advertisement

Answer

Ok, let me explain your problem.

  1. You don’t have configured jdbc driver.
  2. Don’t need to cast anything!
  3. 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)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement