Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 9th, 2007, 10:07 PM
Authorized User
 
Join Date: Oct 2006
Location: Jakarta, , Indonesia.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Nicholsen
Default Cannot insert into databases, the second insertion

Hello
I have a problem about to get auto increment key from PreparedStatement.
The insertion is successful,But after I add ResultSet and getGeneratedKeys()to get autoIncrement key, I compile, it cannot insert data although I had comment the ResultSet, and get autoincrement code. Before i put autoincrement code, my code sometimes work succesful in insertion data, sometimeds failed.

Why this code is not work after I add ResultSet
import java.sql.*;
public class MySqlDataAccess implements DataAccess
{
    private Connection connection;
    private PreparedStatement sqlFind;
    private PreparedStatement sqlInsertUser,sqlInsertPekerjaan,sqlInsertHobby;
    //private Statement sqlInsertUser;
    private PreparedStatement sqlUpdateUser,sqlUpdatePekerjaan,sqlUpdateHobby;
    private PreparedStatement sqlDeleteUser,sqlDeletePekerjaan,sqlDeleteHobby;

    //private ResultSet rs = null;
    public MySqlDataAccess() throws Exception
    {
        connect();
        sqlFind = connection.prepareStatement("select msuser.KodeUser, NamaUser, JenisKelamin, TanggalLahir, " +
                    "NamaPekerjaan,NamaHobby from msuser, mspekerjaan, mshobby where " +
                    "msuser.KodeUser = mspekerjaan.KodeUser and " +
                    "msuser.KodeUser = mshobby.KodeUser and " +
                    "NamaUser = ?");
        sqlInsertUser = connection.prepareStatement("insert into msuser(NamaUser,JenisKelamin,TanggalLahir) " +
                        "values(?,?,?)");//Statement.RETURN_GENERATED_KEYS);
        //sqlInsertPekerjaan = connection.prepareStatement("insert into mspekerjaan(KodeUser,NamaPekerjaan) " +
                        //"values(?,?)");
        //sqlInsertHobby = connection.prepareStatement("insert into mshobby(KodeUser,NamaHobby) " +
                            //"values(?,?)");

    }
    private void connect() throws Exception
    {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost/javasql";
        //try
        //{
            Class.forName(driver);
            connection = DriverManager.getConnection(url,"root","nicholsen" );
            connection.setAutoCommit(false);
        //}
        /*
        catch(SQLException sqlException)
        {
            sqlException.printStackTrace();
            System.exit(1);
        }
        catch(ClassNotFoundException cls)
        {
            cls.printStackTrace();
            System.exit(1);
        }
        */
    }
    public boolean newPerson(InformationEntry person) throws DataAccessException
    {
        try
        {
            int result;
            //int KodeUser = getNewKodeUser();
            //insert into msuser table

            sqlInsertUser.setString(1,person.getNama());
            sqlInsertUser.setString(2,person.getJenisKelamin() );
            sqlInsertUser.setString(3,person.getTanggalLahir() );
            result = sqlInsertUser.executeUpdate();


            if(result == 0)
            {
                connection.rollback();
                return false;
            }
            /*
            int autoIncKeyFromFunc = -1;
            rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");

            if (rs.next()) {
                autoIncKeyFromFunc = rs.getInt(1);
            }

            /*
            //insert into mspekerjaan
            sqlInsertPekerjaan.setInt(1,person.getId());
            sqlInsertPekerjaan.setString(2,person.getPekerjaan ());
            result = sqlInsertPekerjaan.executeUpdate();
            if(result == 0)
            {
                connection.rollback();
                return false;
            }

            //insert into mshobby
            sqlInsertHobby.setInt(1,person.getId());
            sqlInsertHobby.setString(2,person.getHobby());
            result = sqlInsertHobby.executeUpdate();
            if(result == 0)
            {
                connection.rollback();
                return false;
            }
            */
            /*
            sqlInsertUser = connection.createStatement(ResultSet.TYPE_SCROLL_I NSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            result = sqlInsertUser.executeUpdate("INSERT INTO msuser(NamaUser,JenisKelamin,TanggalLahir) " +
                        "VALUES ('" + person.getNama() + "', '" + person.getJenisKelamin() + "', '" + person.getTanggalLahir() + "')");
                        //"VALUES ('Nicholsen','Pria','1986-03-23')");
            if(result == 0)
            {
                connection.rollback();
                return false;
            }
            */
            connection.commit();
            return true;
        }
        catch(SQLException sqlException)
        {
            try
            {
                connection.rollback();
                return false;
            }
            catch(SQLException exc)
            {
                throw new DataAccessException(exc);
            }
        }
    }

    public void close()
    {
        try
        {
            sqlInsertUser.close();
            //sqlInsertPekerjaan.close();
            //sqlInsertHobby.close();

            //rs.close();
            connection.close();
        }
        catch(SQLException sqlException)
        {
            sqlException.printStackTrace();
        }
    }
    protected void finalize()
    {
        close();
    }

}
Thank You
  #2 (permalink)  
Old September 4th, 2007, 07:16 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

what is the exception you are getting?

- Rakesh
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string insertion MunishBhatia ASP.NET 2.0 Professional 1 July 13th, 2007 08:20 AM
Contact Insertion Issue Grafixx01 Access 2 May 30th, 2007 09:17 AM
automatic insertion of new record.....help...... abhit_kumar JSP Basics 1 March 4th, 2005 12:50 PM
error while insertion... abhit_kumar JSP Basics 0 January 24th, 2005 09:09 AM
insertion sort..please amahja56 C++ Programming 1 March 1st, 2004 09:40 PM



All times are GMT -4. The time now is 09:13 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.