![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 Leo Chan ![]()
![]() |
但我沒方執行那方法, Android Studio Compiled Error : Cannot find method connect() 想請問大家是什麼問題嗎? 謝謝 CODING 如下: mssql.java public class mssql { private Boolean _isOpened=false; String ip,db, uid, pwd; public static Connection connect; Statement st; public boolean isOpened() { return _isOpened; } @SuppressLint("NewApi") private Connection connectionHelper(String uid, String pwd, String db, String ip) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); Connection connection = null; String ConnURL = null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); ConnURL = "jdbc:jtds:sqlserver://" + ip + ";" + "databaseName=" + db + ";user=" + uid + ";password=" + pwd + ";"; connection = DriverManager.getConnection(ConnURL); Log.d("MSSQL","Connection"); }catch (SQLException se) { Log.e("ERRO", se.getMessage()); } catch (ClassNotFoundException e){ Log.e("ERRO",e.getMessage()); } catch (Exception e) { Log.e("ERRO", e.getMessage()); } return connection; } public mssql() { try { ip = "192.168.1.114"; db = "discmaster"; uid = "sa"; pwd = "sa654321"; connect = connectionHelper(uid, pwd, db, ip); if (connect.isClosed()==false) { _isOpened = true; System.out.println("connect ok"); } else { _isOpened=false; System.out.println("connect fail"); } } catch (Exception ex) { ex.printStackTrace(); } } } MainActivity.java private Button.OnClickListener myListener = new Button.OnClickListener() { public void onClick(View v ){ switch (v.getId()) { case R.id.btConnect: { try { mssql.connect(); Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show(); } catch(Exception se) { Log.e("SQLERR", se.getMessage()); Toast.makeText(MainActivity.this, "No connection", Toast.LENGTH_SHORT).show(); break; }
搜尋相關Tags的文章:
[ Connect MS SQL EXPRESS ] ,
本篇文章發表於2019-09-09 11:40 |