0

I have a Java program that successfully connects to a mysql database that is hosted on godaddy's server. I can read from that db with out issue, however, when I try to write to it with INSERT or UPDATE for example, the query does not execute. I am using the 'admin' account that I set up through godaddy, I realize this is not the root account. I have checked and verified that the connection is not read only, and have logged out of phpmyadmin while the query ran. I'm not sure what else I can try or if anyone has experienced this issue.

Maybe a setting to the connection I have failed to set? Or maybe its not possible since the db is hosted on godaddy's servers?

Any help is great!

Thanks.

Here is some relevant code:
Connection to db:

Connection con;
public DBconnection(String url, String user, String pass)
{
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection (url,user,pass);


            if(!con.isClosed())
                System.out.println("connecton open");
        } 
        catch (InstantiationException e) {e.printStackTrace();} 
        catch (IllegalAccessException e) {e.printStackTrace();} 
        catch (ClassNotFoundException e) {e.printStackTrace();} 
        catch (SQLException e) {e.printStackTrace();}
}

Send Query:

public ResultSet executeQuery(String query)
{
    ResultSet rs = null;

    try {
        Statement stmt = (Statement) con.createStatement();
        rs = stmt.executeQuery(query);

        //while(rs.next())
            //System.out.println(rs.getString("ticket_num"));
    } 
    catch (SQLException e) {}   

    return rs;
}

Insert Query (works in phpmyadmin):

conn.executeQuery("INSERT INTO tickets VALUES(55555,'12/01/2012','me','reports','test','','','0','Nope')");
5
  • Are you getting any errors? Commented Dec 5, 2012 at 16:59
  • there is some error code? what the console says? Commented Dec 5, 2012 at 17:00
  • 2
    For INSERT statements you have to use executeUpdate, not executeQuery Commented Dec 5, 2012 at 17:01
  • I suggest printing out your exception when you do a query. At the moment, you just let it fall through, so that won't tell you what is happening. Come back with a stack trace and we can have a look. Commented Dec 5, 2012 at 17:03
  • Always insist upon using parameterized SQL to mitigate SQL injection attacks (whatever technology you're using). Commented Dec 5, 2012 at 17:12

2 Answers 2

3

Use, statement.executeUpdate() to do an INSERT query.

FROM API of Statement.executeUpdate(String sql):

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

 executeUpdate("INSERT INTO tickets VALUES(55555,'12/01/2012','me','reports','test','','','0','Nope')");

And i strongly advice you to use PreparedStatemnt rather than simple Statement to perform SQL using JDBC.

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

1 Comment

Thanks for the help! It works now and I'm in the process of studying/switching to prepared statements. Thanks again.
0

The Username and password with which you are instantiating the connection object, only has the permission to read from the database not write. You need to change the credentials for connecting to the database, or you need to provide the permission to the concerned user

Comments

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.