2017-05-08 48 views
0

我想在我的android項目上使用JDBC創建與數據庫的連接。我正在學習如何導入一個jar,然後在activity中創建一個連接的教程。一切正常,但在連接語句我得到一個錯誤Android與JDBC。 createStatement()不存在?

無法解析法「的createStatement()」

,如果我嘗試進入該方法的參考,它說

找不到decleration去

這種方法來自我已經導入的jar? (jtds-1.3.1) 還有什麼我在這裏失蹤?

@Override 
    protected String doInBackground(String... params) 
    { 
     Connection con; 
     String usernam = params[0]; 
     String passwordd = params[1]; 
     if(usernam.trim().equals("")|| passwordd.trim().equals("")) 
      z = "Please enter Username and Password"; 
     else 
     { 
      try 
      { 
       con = connectionclass(un, pass, db, ip);  // Connect to database 
       if (con == null) 
       { 
        z = "Check Your Internet Access!"; 
       } 
       else 
       { 
        // Change below query according to your own database. 
        String query = "select * from owner where mail = '" + usernam.toString() + "' and password = '"+ passwordd.toString() +"' "; 
        Statement stmt = con.createStatement(); 
        ResultSet rs = stmt.executeQuery(query); 
        if(rs.next()) 
        { 
         z = "Login successful"; 
         isSuccess=true; 
         con.close(); 
        } 
        else 
        { 
         z = "Invalid Credentials!"; 
         isSuccess = false; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       isSuccess = false; 
       z = ex.getMessage(); 
      } 
     } 
     return z; 
    } 
} 


@SuppressLint("NewApi") 
public Connection connectionclass(String user, String password, String database, String server) 
{ 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    Connection connection = null; 
    String ConnectionURL = null; 
    try 
    { 
     Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
     ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";"; 
     connection = (Connection) DriverManager.getConnection(ConnectionURL); 
    } 
    catch (SQLException se) 
    { 
     Log.e("error here 1 : ", se.getMessage()); 
    } 
    catch (ClassNotFoundException e) 
    { 
     Log.e("error here 2 : ", e.getMessage()); 
    } 
    catch (Exception e) 
    { 
     Log.e("error here 3 : ", e.getMessage()); 
    } 
    return connection; 
} 

我與close()方法 Here is a screenshot

+0

你有自己的Connection類嗎? – Aryan

+0

是的,我發佈的這段代碼是在一個連接類中。 – Kzaf

回答

2

所有你所要做的就是充分指定類名相同的問題:

java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product","root",""); 

無需鑄造,並您報告的錯誤現在將消失,因爲編譯器將在正確的類中查找該方法。

或者只是重命名你自己的類Connection

+1

我從連接中刪除連接,並將方法類型更改爲java.sql.Connection,錯誤消失。謝謝! – Kzaf