Android ??????jtds?????????????????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android ??????jtds?????????????????????相关的知识,希望对你有一定的参考价值。
?????????import ?????? manage use bean sqlserver sql ?????? adapter
?????????????????????jtds??????jar????????????????????????????????????????????????????????????????????????????????????????????????
1???jtds???jar??????????????????1.2.7???????????????????????????Java??????????????????1.3.1???????????????android??????????????????1.3.1???????????????????????????
2????????????????????????????????????????????????
?????????????????????jtds?????????
Javabean????????????????????????id????????????stuName
package com.ht.jdts;
public class StuDomain {
private String id;
private String stuName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
??????sqlserver????????????
package com.example.jdtsproc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class SqlHelper {
private String drive = "net.sourceforge.jtds.jdbc.Driver";
private String connStr;
private String server;
private String dbName;
private String userName;
private String userPwd;
private Connection con;
private PreparedStatement pstm;
private ResultSet rs;
private Statement stmt;
public SqlHelper(String server, String dbName, String userName,
String userPwd) {
this.server = server;
this.dbName = dbName;//????????????
this.connStr = "jdbc:jtds:sqlserver://" + this.server + ":1433/"
+ this.dbName;
this.userName = userName;
this.userPwd = userPwd;
}
public List<StuDomain> ExecuteQuery(String sql) {
List<StuDomain> stuLists = new ArrayList<StuDomain>();
try {
Class.forName(drive);
System.out.println("??????????????????");
con = DriverManager.getConnection(this.connStr, this.userName,
this.userPwd);
System.out.println("?????????????????????");
//pstm = con.prepareStatement(sql);
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
StuDomain stuDomain=new StuDomain();
stuDomain.setId(rs.getString(1));
stuDomain.setStuName(rs.getString(2));
stuLists.add(stuDomain);
}
System.out.println("stuLists.size():"+stuLists.size());
} catch (Exception e) {
System.out.println("?????????????????????");
e.printStackTrace();
return null;
} finally {
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stuLists;
}
}
MainActivity ?????????????????????ListView???
package com.example.jdtsproc;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button btn_connect;
private ListView lv_lists;
private List<StuDomain> stuLists;
private SqlHelper sh=new SqlHelper("xxx.xx.xxx.xx", "MyDb1", "sa", "sa");//??????????????????????????????????????????????????????ip
private Handler mHandler=new Handler(){
public void handleMessage(android.os.Message msg) {
lv_lists.setAdapter(new StuAdapter());
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
btn_connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(){
public void run() {
String sql="select *from stu";
stuLists = sh.ExecuteQuery(sql);
mHandler.sendEmptyMessage(0);//????????????
//lv_lists.setAdapter(new StuAdapter());
};
}.start();
}
});
}
private void initView() {
//????????????????????????
btn_connect = (Button) findViewById(R.id.btn_connect);
lv_lists = (ListView) findViewById(R.id.lv_lists);
}
class StuAdapter extends BaseAdapter{
@Override
public int getCount() {
return stuLists.size();
}
@Override
public StuDomain getItem(int position) {
return stuLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
StuViewHolder stuHolder;
if(convertView==null){
convertView=View.inflate(getApplicationContext(), R.layout.list_main, null);
stuHolder=new StuViewHolder();
stuHolder.tv_id=(TextView) convertView.findViewById(R.id.tv_id);
stuHolder.tv_stuname=(TextView) convertView.findViewById(R.id.tv_stuname);
stuHolder.iv_photos=(ImageView) convertView.findViewById(R.id.iv_photos);
}else{
stuHolder=(StuViewHolder) convertView.getTag();
}
stuHolder.tv_id.setText(getItem(position).getId());
stuHolder.tv_stuname.setText(getItem(position).getStuName());
stuHolder.iv_photos.setImageResource(R.drawable.photopager);
return convertView;
}
}
class StuViewHolder{
public TextView tv_id;
public TextView tv_stuname;
public ImageView iv_photos;
}
}
????????????jtds?????????jar??? ???????????????https://pan.baidu.com/s/1YBW1I_ddSKe7vcIouDXKrg ?????????0v95
以上是关于Android ??????jtds?????????????????????的主要内容,如果未能解决你的问题,请参考以下文章
如何使用库 jTDS JDBC 驱动程序将 android 应用程序连接到 sql server 2012
空对象引用上的 Android Studio jTDS java.lang.NullPointerException java.sql.Statement java.sql.Connection.cr
带有 Service Pack 2 的 SQL Server Developer Edition 的 jTDS 连接字符串
打开跟踪文件时出错:没有这样的文件或目录 | java.lang.ClassNotFoundException:net.sourceforge.jtds.jdbc.Driver