简体   繁体   English

如何使用库jTDS JDBC驱动程序将android应用程序连接到sql server 2012

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

I am a new member, android developer. 我是Android开发人员的新成员。 When i use library jTDS JDBC Driver (jtds-1.2.7) to make a connection to sql server, I recieve this error: 当我使用库jTDS JDBC驱动程序(jtds-1.2.7)建立与SQL Server的连接时,我收到此错误:

    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. 您正在尝试连接到127.0.0.1。 That's yourself. 就是你自己 I doubt that the db is on your phone, since it isn't SQLite. 我怀疑数据库不在您的手机上,因为它不是SQLite。 Fix the IP address. 固定IP地址。

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. 它要求您允许任何IP连接到数据库。 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. 更好的方法是在两者之间放置一个Web服务,然后仅允许该PC直接连接到数据库。 It makes it more difficult to gain access to your data. 这使得获取您的数据更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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