package com.rockcheck.mes; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import java.sql.*; public class PDAActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pda_home); } //查询oracle数据库 public void KunHaoQuery(View view) { new QueryOracle().execute(); } class QueryOracle extends AsyncTask<Void, Integer, String> { String result = null; @Override protected String doInBackground(Void... voids) { result = Query(); return result; } @Override protected void onPostExecute(String str) { Toast.makeText(PDAActivity.this, result, Toast.LENGTH_SHORT).show(); } } public String Query() { String wuliao = "123"; String user = "MES"; String password = "zidonghua"; String url = "jdbc:oracle:thin:@10.0.2.2:1521:orcl"; String sql = "select AMOUNT from FRUITS where id=1"; // 创建一个数据库连接 Connection connection = null; Statement pstm = null; // 创建一个结果集对象 ResultSet rs = null; try { //Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序 connection = DriverManager.getConnection(url, user, password); pstm = connection.createStatement(); rs = pstm.executeQuery(sql); if (rs != null) { while (rs.next()) { wuliao = rs.getString("AMOUNT"); } } } catch (Exception e) { e.printStackTrace(); } return wuliao; } }