Chris K
Chris K

Reputation: 1629

Java program will read from database, but not write to it

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')");

Upvotes: 0

Views: 921

Answers (2)

PermGenError
PermGenError

Reputation: 46428

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.

Upvotes: 3

Sashi Kant
Sashi Kant

Reputation: 13465

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

Upvotes: 0

Related Questions