简体   繁体   English

如何将Android与SQL Server数据库连接?

[英]How to connect Android with SQL server database?

I am getting problem in my application 我的应用程序出现问题

Here is my piece of code my activity throwing an exception "Error Connection net.sourceforge.jtds.jdbc.Driver" 这是我的活动抛出异常“ Error Connection net.sourceforge.jtds.jdbc.Driver”的代码段

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sync = (Button) findViewById(R.id.button1);
        sync.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                query2(); }
        });
        }
    public void query2()
    {
    Log.i("Android"," SQL Connect Example.");
    Connection conn = null;
    try {
    String driver = "net.sourceforge.jtds.jdbc.Driver";
    Class.forName(driver).newInstance();
    //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
    String connString = "jdbc:jtds:sqlserver://server_ip_address         :198.168.0.10/tracebaledb;encrypt=fasle;user=sa;password=SqlServer2005;instance=SQLEXPRESS;";
    String username = "sa";
    String password = "SqlServer2005";
    conn = DriverManager.getConnection(connString,username,password);
    Log.w("Connection","open");
    Statement stmt = conn.createStatement();
    ResultSet reset = stmt.executeQuery("select * from AD_Login");

    //Print the data to the console
    while(reset.next()){
    Log.w("Data:",reset.getString(3));
                  Log.w("Data",reset.getString(2)); }
    conn.close();

    } catch (Exception e)
    {
    Log.w("Error connection","" + e.getMessage());
    }
    }    
}

You can connect to SQL Server databse throuh webservice. 您可以通过Web服务连接到SQL Server数据库。

Here is the sample code to call the webservice. 这是调用Web服务的示例代码。

String url              = "your_webservice_URL";

 try 
 {
    HttpPost loginHttpPost   = new HttpPost(url); 
    HttpContext localContext = new BasicHttpContext();          

    MultipartEntity multipartContent = new MultipartEntity();
    multipartContent.addPart("parameter1", new StringBody(value1));
    multipartContent.addPart("parameter2", new StringBody(value2));
    loginHttpPost.setEntity(multipartContent);

    HttpClient objHttpClient = new DefaultHttpClient();
    HttpResponse response = objHttpClient.execute(loginHttpPost,localContext);
 } 
 catch (IOException e) {
     e.printStackTrace();

Make sure you set the permission in Manifest.xml file, 确保在Manifest.xml文件中设置了权限,

You can write the webservice in php or java to get this values and connect to SQL Server 您可以使用php或java编写Web服务以获取此值并连接到SQL Server

An other approach is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the SQL Server JDBC Driver. 另一种方法是使用使用三层体系结构的虚拟JDBC驱动程序:将JDBC代码通过HTTP发送到远程Servlet,该远程Servlet在将JDBC代码(配置和安全性)传递给SQL Server JDBC Driver之前对其进行过滤。 The result is sent you back through HTTP. 结果通过HTTP发送回给您。 There are some free software that use this technique. 有一些使用此技术的免费软件。 This is secure because the database is never exposed directly and you may use SSL. 这是安全的,因为永远不会直接公开数据库,并且您可以使用SSL。 Just Google "Android JDBC Driver over HTTP". 只是Google“基于HTTP的Android JDBC驱动程序”。

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

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