2012-11-24 81 views
1

我想通過JDBC到我的Android應用程序連接到我的PostgreSQL數據庫,登錄順利,但是當我嘗試運行查詢explotes,給我這個錯誤:的jdbc PostgreSQL是不執行查詢

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

     } 

    } 

回答

1

你需要調用executeUpdate,不executeQuery

int numRowsAffected = st.executeUpdate("INSERT INTO order_tbl (custumer, _name, additional, ingredients_) VALUES('adsfasdf','asdfasdfasdf', 'asdfasdfasd', 'asdfasdfasdf', 'dasfasdf')"); 
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插入http://www.youtube.com/watch?v=Zto0PovkbKo

+0

它可以爲Java與Android的出快速教程,但是當我嘗試從Android做是行不通的。 – hidura