kindly help with this code(prepare statement)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 37

Thread: kindly help with this code(prepare statement)

  1. #1
    Join Date
    Apr 2006
    Posts
    28

    Unhappy kindly help with this code(prepare statement)

    kindly help
    cheers
    Last edited by indiewolf; 04-15-2006 at 12:54 PM.

  2. #2
    Join Date
    Mar 2006
    Posts
    28
    first of all you should really examine your code
    you learn from your mistakes

    now the errors:

    errors 1-3:
    these errors all have the same question: where is the variable 'request'
    where did you declare it and which type is it?

    error 4:
    cannot find symbol
    symbol: variable out

    if you want to write to the console you should use:
    System.out.println("...");

    errors 5-6:
    cannot find symbol
    symbol: variable connection

    in the try block you declared: 'Connection conn= DriverManager.getConnection(url,"root","mysql");'
    although you declared it here, it isn't available in the finally block because all blocks are seperated from each other and therefore cannot use the same variables, unless you declare your variable at a level above these blocks

    like f.e. :
    public static void main(String[] args) {
    String loginid= request.getParameter("loginid");
    String password = request.getParameter("password");
    String email = request.getParameter("email");
    Connection conn;
    try{...}
    catch {...}
    finally {...}
    ...

    also in your finally block you used 'connection' and it should be 'conn'

  3. #3
    Join Date
    Apr 2006
    Posts
    28
    first of all thanks a lot for replying ice cube c

    errors 4 and 5 are solved..my bad coz they went unnoticed.

    bout 6 thi s is the error

    cannot find symbol
    method close()

    and and still not able to figure out how u deal with errors 1,2,3??

    how do i declare the variable request?

    kindly help
    thank u

    the code now
    Last edited by indiewolf; 04-15-2006 at 12:55 PM.

  4. #4
    Join Date
    Mar 2006
    Posts
    28
    Code:
    cannot find symbol 
    method close()
    this error occurs because you've declared conn as an 'object'
    it should be a 'Connection'

    so you should change it to:

    Code:
    package reg;
    import java.sql.*;
    
    
    public class register {
    
        private static Connection conn;
    
    public static void main(String[] args) {
           
         
            String loginid = request.getParameter("loginid");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            
            try {
                
                // Step 1: Load the JDBC driver.
                Class.forName("org.gjt.mm.mysql.Driver");
                
                // Step 2: Establish the connection to the database.
                String url = "jdbc:mysql://localhost/login";
                conn = DriverManager.getConnection(url,"root","mysql");
                
                PreparedStatement st;
               
                
                st = conn.prepareStatement("INSERT INTO register(loginid,password,email) VALUES (?,?,?)");
                st.setString(1,loginid);
                st.setString(2,password);
                st.setString(3,email);
                st.executeUpdate();
            }catch(Exception ex){
                System.out.println("Successfull Update");
            }finally{
                if ( conn!=null){
                    conn.close();
                }
            }
        }
    }
    but then you're still stuck with the errors concerning 'request'

    can you explain to me why you use this code:
    request.getParameter()

    if you want data to be inserted by a user, I suggest that you use JOptionPane

    then it would look like this:

    before you use JOptionPane, you must import the right class:
    import javax.swing.JOptionPane;

    Code:
    String loginid = JOptionPane.showInputDialog("Insert your login id");
    String password = JOptionPane.showInputDialog("Insert your password");
    String email = JOptionPane.showInputDialog("Insert your email");
    you will be prompted with 3 dialogues, in which you can input text

  5. #5
    Join Date
    Apr 2006
    Posts
    28
    thanks once again.

    kindly help
    cheers
    Last edited by indiewolf; 04-15-2006 at 12:55 PM.

  6. #6
    Join Date
    Mar 2006
    Posts
    28
    try this:

    ...

    PreparedStatement st;
    ResultSet rs = null;

    st = conn.prepareStatement("INSERT INTO register(loginid,password,email) VALUES (?,?,?)");
    st.setString(1,loginid);
    st.setString(2,password);
    st.setString(3,email);

    rs = st.executeUpdate();


    ...

  7. #7
    Join Date
    Apr 2006
    Posts
    28
    its gives the following error for

    rs = st.executeUpdate();


    incompatible types
    found : int
    required: java.sql.ResultSet
    rs = st.executeUpdate();
    1 error


    kindly help

  8. #8
    Join Date
    Mar 2006
    Posts
    28
    my mistake the code I provided doesn't belong to PreparedStatement but to Statement

    I think the reason why there's no input in your database is because you didn't close your PreparedStatement

    try this:

    Code:
    ...
    
    public static void main(String[] args) {
    
    
            Connection conn = null;
            PreparedStatement st = null;
    
            ...
    
            finally{
                if ( conn!=null){
                    try{
                        st.close();
                        conn.close();
                    } 
                    catch (SQLException ex){
                        ex.printStackTrace();
                    }
                }
            }
    
    ...
    oh and remove the following line in your try block:
    PreparedStatement st;
    this is already declared after Connection
    Last edited by IceCubeC; 04-06-2006 at 04:11 AM.

  9. #9
    Join Date
    Apr 2006
    Posts
    28
    hi ,
    first of all thanks.

    but now again i have the same problem.
    although the code is error free,
    it still does not update the database.
    the database table is still empty


    i repeat that i had built the database using netbeans>runtime explorer>databases
    jdbc:mysql://localhost:3306/register

    table name register
    loginid password email

    i input values in the form in register.html, on clicking submit it takes me to register.jsp, where register.java class also runs(before).

    the register.jsp page is displayed..no messages..nothing.
    neither the database is updated.

    i am totally pissed off..kindly help

    cheers

  10. #10
    Join Date
    Mar 2006
    Posts
    28
    try this driver instead:

    Class.forName("com.mysql.jdbc.Driver");

    as for netbeans database configurations
    did you do everything according to this page:
    http://www.netbeans.org/kb/41/using-...ns/dbconn.html

    and which server are you using? TomCat, Apache
    Last edited by IceCubeC; 04-06-2006 at 05:57 AM.

  11. #11
    Join Date
    Apr 2006
    Posts
    28
    well i did try that page before.and i actually learnt communicating with databases in netbeans through it. i was able to run the jsp.
    but now since u are telling me to change the driver, i wud do it again, and let u know ,asap.
    cheers

  12. #12
    Join Date
    Apr 2006
    Posts
    28
    well these are the three files used, along with a home page.
    now the netbeans database(register) consists of loginid,password,email columns in a table called reg.

    I STILL have the same problem. the register database is not updated with the values inserted in register.html form.


    kindly help.

    cheers
    Last edited by indiewolf; 04-15-2006 at 12:56 PM.

  13. #13
    Join Date
    Mar 2006
    Posts
    28
    the reason why there's no input in your database is because you're using a html form, so JOptionPane that I suggested has no use here

    I thought that you wanted to run the application, not a html file

    can you try to run the appliction in stead of the html file
    do this with the driver I suggested

    compile it first
    -> javac register.java

    then execute it:
    -> java register

    let me know what happened

  14. #14
    Join Date
    Apr 2006
    Posts
    28
    C:\>cd java

    C:\Java>cd a

    C:\Java\a>dir


    Directory of C:\Java\a

    03/03/2006 05:13 <DIR> .
    03/03/2006 05:13 <DIR> ..
    03/03/2006 05:13 <DIR> src
    03/04/2006 04:29 3,023 build.xml
    03/03/2006 05:13 <DIR> nbproject
    03/03/2006 05:13 <DIR> web
    03/03/2006 05:13 <DIR> test
    06/04/2006 13:20 <DIR> build
    06/04/2006 13:20 <DIR> dist
    1 File(s) 3,023 bytes
    8 Dir(s) 1,028,407,296 bytes free

    C:\Java\a>cd src

    C:\Java\a\src>dir



    Directory of C:\Java\ar\src\java\reg

    06/04/2006 08:27 <DIR> .
    06/04/2006 08:27 <DIR> ..
    06/04/2006 13:20 1,596 register.java
    1 File(s) 1,596 bytes
    2 Dir(s) 1,028,407,296 bytes free

    C:\Java\a\src\java\reg>javac register.java

    C:\Java\a\src\java\reg>java register
    Exception in thread "main" java.lang.NoClassDefFoundError: register (wrong name:
    reg/register)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)


    so this is what i did. just to show how i did it , i have included everything.

    well kindly lemme know why the error and how can i do it on the webpage, cause ultimately i have to do that.

    cheers

  15. #15
    Join Date
    Mar 2006
    Posts
    28
    remove package reg; at the top

    also do this:

    ...

    catch(Exception ex)
    {
    ex.printStackTrace();
    }

    ...
    Last edited by IceCubeC; 04-06-2006 at 03:23 PM.

Similar Threads

  1. Replies: 2
    Last Post: 05-18-2003, 10:16 PM
  2. Returning a sql statement...
    By Chiodo in forum Database
    Replies: 0
    Last Post: 05-21-2002, 10:23 AM
  3. Does VB.NET have a C# like "using" statement?
    By Cali LaFollett in forum .NET
    Replies: 24
    Last Post: 09-16-2001, 03:43 PM
  4. Access And SQL date statement
    By Saiful in forum VB Classic
    Replies: 7
    Last Post: 11-24-2000, 05:21 AM
  5. Select statement in ASP
    By Victor Tai in forum Web
    Replies: 1
    Last Post: 07-24-2000, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center