Information About Adding Information to Java MySQL

Asked 2 years ago, Updated 2 years ago, 45 views

Data Manipulation Class

public class Toi3_db{
    /**
     * @paramargs
     * @throws SQLException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[]args)rows SQLException, ClassNotFoundException {
        Member mem = new Member (11, "Yamasaki", "[email protected]");
        Connection con=(new DBC Connection()) .getConnection();
        MemberCtrl memctrl = new MemberCtrl(con);
        boolean ret = memctrl.excuteInsert(mem);

        if(ret==false){
            // Output processing in case of failure
            System.out.println("INSERT Failure:");
        }
    }
}

Class to Handle

public class MemberCtrl{

    public boolean executeInsert (Member mem) threads SQLException, ClassNotFoundException {
        try{
            // Define SQL statements
            String sql = "INSERT INTOT_MEMBER(id, name, email) VALUES(?,?,?)";
            System.out.println("a");
            // Specify SQL statements and parameters to run
            ps = con.prepareStatement(sql);
            System.out.println("b");
            ps.setInt(1,11);
            ps.setString(2, "yamasaki");
            ps.setString(3, "[email protected]");

            // Execute INSERT statements
            ps.executeUpdate();
        }finally {
            if(con!=null){
                con.close();
            }   
        }

        return false;
    }
}

Run to

Exception in thread "main" java.lang.NullPointerException
at MemberCtrl.excuteInsert (MemberCtrl.java:46)
at Toi3_db.main(Toi3_db.java:21)

Toi3_db.java:21 is boolean ret=memctrl.excuteInsert(mem);
MemberCtrl.java:46 is ps=con.prepareStatement(sql);

Why did it happen? Thank you for your cooperation.

additional:

Thank you.The previous error was resolved by adding the constructor.
But now

Exception in thread "main" java.sql.SQLException: Parameter index out of range (1>number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
atctrl.MemberCtrl.excuteInsert (MemberCtrl.java:66)
at Toi3_db.main(Toi3_db.java:21)

MemberCtrl.java:66 is ps.setInt(1,11);
Toi3_db.java:21 is boolean ret=memctrl.excuteInsert(mem);

The error occurred. Why?

java mysql

2022-09-30 19:34

1 Answers

The error added indicates that the number of parameters that the preparedStatement can receive is 0, but setInt() specifies 1 for the parameter index.In other words, there is a high probability that there is something wrong with the specified SQL.

MySQL is case sensitive to table names depending on the environment and settings, but does that match the suggested source?


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.