简体   繁体   中英

How to connect android application to sql server 2012 using library jTDS JDBC Driver

I am a new member, android developer. When i use library jTDS JDBC Driver (jtds-1.2.7) to make a connection to sql server, I recieve this error:

    unknown server host name unable to resolve host "127.0.0.0.1\sqlexpress": No address associated with hostname

Here is the source code:

Connection connection ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connection=CONN("sa", "abc123", "SqlForAndroid", "127.0.0.1\\sqlexpress:1433");
}

@SuppressLint("NewApi")
private Connection CONN(String _user, String _pass, String _DB, String _server )
{
StrictMode.ThreadPolicy policy = new    StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {

    Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
    ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"+"databaseName=" + _DB + ";user=" + _user + ";password=" + _pass +";";
    conn = DriverManager.getConnection(ConnURL);        
} catch (SQLException se) {
    Log.e("ERRO",se.getMessage());
} catch (ClassNotFoundException e) {
    Log.e("ERRO",e.getMessage());
} catch (Exception e) {
    Log.e("ERRO",e.getMessage());
}

return conn;

}

I need a solution...

You're trying to connect to 127.0.0.1. That's yourself. I doubt that the db is on your phone, since it isn't SQLite. Fix the IP address.

As an aside- connecting directly via mobile to a db is a bad idea from a security perspective. It requires you to allow any IP to connect to your db. A better way to do it is to put a webservice in between, and then only allow that PC to connect directly to the db. It makes it more difficult to gain access to your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM