hidura
hidura

Reputation: 693

Jdbc Postgresql is not executing the query

I am trying to connect my android app to my postgresql database through jdbc, the login goes well but when i try run the query explotes and give me this error:

11-24 11:03:14.966: E/AndroidRuntime(673):  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)
11-24 11:03:14.966: E/AndroidRuntime(673):  at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:283)

This is the code of the function that make the connection:

public void save_info(Element plate_info){
         String retval = "";
         try {
             Class.forName("org.postgresql.Driver");
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
             retval = e.toString();
         }
         String url = "jdbc:postgresql://test.com:5432/test_db?user=adsfsdfasd&password=2222222";
         Connection conn;
         try {
             DriverManager.setLoginTimeout(5);
             conn = DriverManager.getConnection(url);
             Statement st = conn.createStatement();
             ResultSet rs = st.executeQuery("INSERT INTO order_tbl (custumer, _name, additional, ingredients_) VALUES('adsfasdf','asdfasdfasdf', 'asdfasdfasd', 'asdfasdfasdf', 'dasfasdf')");
             while(rs.next()) {
                 retval = rs.getString(1);
             }
             rs.close();
             st.close();
             conn.close();
             System.out.println(retval);
         } catch (SQLException e) {
             e.printStackTrace();
             retval = e.toString();

         }

    }

Upvotes: 1

Views: 1895

Answers (2)

gmustudent
gmustudent

Reputation: 2209

This is all you need.

DriverManager.setLoginTimeout(5);
conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
st.execute("INSERT INTO order_tbl (custumer, _name, additional, ingredients_) VALUES('adsfasdf','asdfasdfasdf', 'asdfasdfasd', 'asdfasdfasdf', 'dasfasdf')");

Here's a quick tutorial on how to use SQL Insert http://www.youtube.com/watch?v=Zto0PovkbKo

Upvotes: 1

duffymo
duffymo

Reputation: 308898

You need to call executeUpdate, not executeQuery

int numRowsAffected = st.executeUpdate("INSERT INTO order_tbl (custumer, _name, additional, ingredients_) VALUES('adsfasdf','asdfasdfasdf', 'asdfasdfasd', 'asdfasdfasdf', 'dasfasdf')");

Upvotes: 1

Related Questions