0

I'm using Eclipse EE, Tomcat 6 server connected to MySQL server on win7. I was able to connect to a database through JSP and send commands like SELECT and DESCRIBE but if I try to create a table the JSP code will simply not run! Why is that? (Checked my SQL codes on the MySQL command line and it worked. )

JSP error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at

JSP Coding:

try // DB login
    {
        String connectionURL = "jdbc:mysql://localhost/ofir";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(connectionURL, "root", "");
        stmt = con.createStatement();
        connected = true;
    }
    catch(Exception ex)
    {
        out.println("Unable to connect to database.");
        connected = false;
    }
    rs = stmt.executeQuery("CREATE table users (fname varchar(50), nname varchar(20), pass varchar(50), email varchar(50), gender boolean, age int(3), region varchar(10), notes varchar(1000))"); // Line of the error

*To be noted about the code- all variables are declared globally using ' <%! '

1 Answer 1

2

For CREATE TABLE, we have to use stmt.executeUpdate(sql) . Not stmt.executeQuery(sql)

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, so at what situations do I need to use one of them?
It retuns an int. The int represents the number of rows that were affected. But its only applicable for statements where you INSERT or UPDATE rows. For your case (CREATE Table), it will always return 0 because it didn't really change any rows. You can ignore the return value for your case.
Here is the actual documentation : docs.oracle.com/javase/7/docs/api/java/sql/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.