Jdbc Postgresql没有执行查询

时间:2012-11-24 15:19:17

标签: java android postgresql jdbc

我正在尝试通过jdbc将我的Android应用程序连接到我的postgresql数据库,登录顺利但是当我尝试运行查询explotes并给我这个错误时:

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)

这是建立连接的函数的代码:

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();

         }

    }

2 个答案:

答案 0 :(得分:1)

您需要致电executeUpdate,而不是executeQuery

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

答案 1 :(得分:1)

这就是你所需要的一切。

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

以下是有关如何使用SQL Insert http://www.youtube.com/watch?v=Zto0PovkbKo

的快速教程