我已经创建了一个ConnectionClass.java,其中设置了服务器名称,数据库和其他服务器,但我的问题出在MainActivity.java上,如何从sql server显示数据,我有表名" TableStatus&#34 ;行是TableName,TransactionNumber和Status
这是MainAcitivity.java的源代码
public class MainActivity extends AppCompatActivity {
ConnectionClass connectionClass;
ListView lstpro;
String proid;
String z = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
lstpro = (ListView) findViewById(R.id.lstproducts);
}
// View ?
public class FillList extends AsyncTask<String, String, String> {
String z = "";
List<Map<String, String>> prolist = new ArrayList<Map<String, String>>();
@Override
protected void onPostExecute(String r) {
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
String[] from = { "A", "B", "C" };
int[] views = { R.id.lblproid, R.id.lblproname,R.id.lblprodesc };
final SimpleAdapter ADA = new SimpleAdapter(MainActivity.this,
prolist, R.layout.lsttemplate, from,
views);
lstpro.setAdapter(ADA);
lstpro.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
HashMap<String, Object> obj = (HashMap<String, Object>) ADA
.getItem(arg2);
proid = (String) obj.get("A");
String proname = (String) obj.get("B");
String prodesc = (String) obj.get("C");
//edtprodesc.setText(prodesc);
// edtproname.setText(proname);
// qty.setText(qtys);
}
});
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from TableStatus";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
ArrayList<String> data1 = new ArrayList<String>();
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("A", rs.getString("TableID"));
datanum.put("B", rs.getString("TableName"));
datanum.put("C", rs.getString("Status"));
prolist.add(datanum);
}
z = "Success";
}
} catch (Exception ex) {
z = "Error retrieving data from table";
}
return z;
}
}
}
任何帮助都会在android开发上做初学者
答案 0 :(得分:0)
DBconnection.Getconnection()
此处的解释Connection Demo
private void DBSyncData() {
try {
Connection connect = DBconnection.Getconnection();
Statement cat = connect.createStatement();
ResultSet category = cat.executeQuery("Select * from TableStatus");
while (category.next()) {
String col1 = category.getString("ColumName1"));
String col2 = category.getString("ColumName2"));
//....
} catch (Exception e) {
}
}
<强>更新强>
我的工作代码注意我使用的旧版本的jar jtds 1.2.7
文件存在最新版本的问题。
public class DBconnection {
public static String User_name = "saxxxx";
public static String User_pass = "xxxxxh";
public static String Db_name = "xxxxx";
static Context mcontext;
public static Connection Conn = null;
public static String Server_ip = "192.168.5.14:1433;";
public static Connection Getconnection() {
Log.i("Android", " MySQL Connect.");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
String connString = null;
try {
Log.i("SQL Looking", "Start Looking for server");
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
connString = "jdbc:jtds:sqlserver://" + Server_ip
+ ";databaseName=" + Db_name + ";user=" + User_name
+ ";password=" + User_pass + ";";
Conn = DriverManager.getConnection(connString);
Log.i("Connection", "open DB Class");
} catch (Exception e) {
Toast.makeText(mcontext, e.toString() + "db", Toast.LENGTH_LONG)
.show();
Log.w("Error connection", e.getMessage());
}
return Conn;
}
public static void close_DB() {
try {
Conn.close();
Log.i("Connection", "Close DB Class");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}