Android uses JTDS remote Access database

Source: Internet
Author: User

Recently the teacher asked me to use Jtds this jar package to remotely access the data in the database, although not difficult, but there are a few points to pay attention to.

1, Jtds jar Package I use 1.2.7 this version, if the Java project uses 1.3.1 This version, Android project can not use 1.3.1 otherwise connected to the database

2, the remote database server firewall to shut down

Here's a case of how to use Jtds

JavaBean has two fields one is ID, one is 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;
}
}

Tools class for accessing SQL Server

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;//database name
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 ("Driver loading succeeded");
con = drivermanager.getconnection (this.connstr, This.username,
THIS.USERPWD);
SYSTEM.OUT.PRINTLN ("Database connection succeeded");
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 ("Database connection exception");
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 and displays the data on the 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");//The first parameter is the IP of the remote database server that is accessed
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);//Send Message
Lv_lists.setadapter (New Stuadapter ());
};
}.start ();
}
});
}

private void Initview () {
Find what we need.
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;
}

}

Last dedicated Jtds two jar bundle permalink: https://pan.baidu.com/s/1YBW1I_ddSKe7vcIouDXKrg password: 0v95

Android uses JTDS remote Access database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.