Chúng ta sẽ sử dụng thư viện JTDS để connect tới SQL DB.
Link down thư viện JTDS đây.
Các bước chuẩn bị đã xong chúng ta đi và code nhé.
public void connectToDB() {
try {
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
String username = "sa";
String password = "1234567";
Connection DbConn = DriverManager
.getConnection(
"jdbc:jtds:sqlserver://YOUR_IP_ADRESS:1433/YOUR_DB_NAME;encrypt=false;instance=SQLEXPRESS;",
username, password);
Log.w("Connection", "open");
Statement stmt = DbConn.createStatement();
ResultSet reset = stmt.executeQuery(" select * from mst商品");
while (reset.next()) {
Log.w("Data:", reset.getString(1));
}
DbConn.close();
} catch (Exception e) {
Log.e("Data:", e.getMessage());
}
}
Các bạn nhớ thêm permission trong file AndroidManifest nhé.
Chúc các bạn thành công !
Sign up here with your email
ConversionConversion EmoticonEmoticon