当前位置:文档之家› android studio连接SQL数据库

android studio连接SQL数据库

super.onPreExecute(); } protected String doInBackground(Integer... params) {
try { Class.forName( "net.sourceforge.jtds.jdbc.Driver");
connUrl="jdbc:jtds:sqlserver://116.228.80.90:9033;databaseName=acrel;user=sa;password=acrel; ";
public class MainActivity extends Activity {
String connUrl=null; @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(yout.activity_main); ConnectTask dt = new ConnectTask(); dt.execute(); } class ConnectTask extends AsyncTask<Integer, Integer, String> { Connection con = null; protected void onPreExecute() {
Thread.sleep(300); Log.e("&&&&&", rs.getString("Time")); Log.e("&&&&&", rs.getString("TagName")); Log.e("&&&&&", rs.getString("PV")); } rs.close(); stmt.close(); } catch (SQLException se) { System.out.println(se.getMessage().toString()); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (con != null) try {
con.close(); } catch(SQLException se) { } } } }
}
try {
String sql = "SELECT TOP 10 * FROM dbo.15minuter"; Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); e com.example.mysql; import android.os.Bundle; import android.app.Activity; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import android.os.AsyncTask; import android.util.Log;
con = DriverManager.getConnection(connUrl); testConnection(con); } catch(SQLException se){ Log.e("ERROR", se.getMessage()); }catch(ClassNotFoundException cl){ Log.e("ERROR", cl.getMessage()); }catch(Exception e){ Log.e("ERROR", e.getMessage()); } return "finish";
} protected void onPostExecute(String result) {
super.onPostExecute(result); } public void testConnection(Connection con) throws java.sql.SQLException {
相关主题