开发者

Exception in Database

开发者 https://www.devze.com 2022-12-16 09:17 出处:网络
HI I made a method in my class that is: public void addInfo(String username, String password, String name, String family) {

HI

I made a method in my class that is:

public void addInfo(String username, String password, String name, String family) {
        try {

 String sql = "INSERT INTO user VALUES (?, ?, ?, ?)";
            PreparedStatement pst = conn.prepareCall(sql);
            pst.setString(1, username);
            pst.setString(2, password);
            pst.setString(3, name);
            pst.setString(4, family);
            int nRecords = pst.executeUpdate();
            logger.inf开发者_如何学编程o("User Added Successfully...");
        } catch (SQLException ex) {
            logger.info("ERROR in adding user");

            ex.printStackTrace();
        }
    }

And then I retrieve this in my addInfo class:

 private void AcceptButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        String name = NameField.getText().trim();
        String family = FamilyField.getText().trim();
        String username_signup = UsernameSignUp.getText().trim();
        String password = new String(PasswordSignUp.getPassword()).trim();

        if ((name == null ? "" == null : name.equals("")) || (family == null ? "" == null : family.equals("")) || (username_signup == null ? "" == null : username_signup.equals("")) || (password == null ? "" == null : password.equals(""))) {

            JOptionPane.showMessageDialog(this, "Fill the blanks...", "WARNING", JOptionPane.WARNING_MESSAGE);
            clearAll();
            NameField.grabFocus();
            return;

        } else {

            UserManager um = new UserManager();
            um.addInfo(username_signup, password, name, family);

            FirstPage fp = new FirstPage();
            this.setVisible(false);
            fp.setVisible(true);
        }
    }

But I face with these exceptions:

run:
Jan 9, 2010 3:54:20 PM DBManager.UserManager addInfo
INFO: ERROR in adding user
java.sql.SQLException: Unable to retrieve metadata for procedure.
        at com.mysql.jdbc.CallableStatement.extractProcedureName(CallableStatement.java:1076)
        at com.mysql.jdbc.CallableStatement.determineParameterTypes(CallableStatement.java:1026)
        at com.mysql.jdbc.CallableStatement.<init>(CallableStatement.java:83)
        at com.mysql.jdbc.Connection.prepareCall(Connection.java:1204)
        at com.mysql.jdbc.Connection.prepareCall(Connection.java:1181) 


conn.prepareCall(sql) should only be used for invoking stored procedures, not raw SQL.

Replace your prepareCall with prepareStatement, and you should be fine.


You need to use prepareStatement(). prepareCall() is used for calling stored procedures.


The error is in prepareCall. It's only used for stored procedures. You should use prepareStatement instead.


Just one more point: you aren't cleaning up resources at all. You've got a PreparedStatement that's eligible for GC when you exit the block, but you don't close it anywhere. Close it in a finally block using a static method that doesn't throw an exception:

public void insert(Connection connection)
{
    PreparedStatement ps = null;
    try
    {   
        // SQL stuff here.
    }
    finally
    {
        close(ps);
    }
}

public static void close(Statement s)
{
    try
    {  
        if (s != null)
        {
            s.close();
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
}

Same holds true for Connection and ResultSet.

No transactional logic for you, either. Usually you want to rollback write operations in the event of a failure.

0

精彩评论

暂无评论...
验证码 换一张
取 消