将android应用程序连接到Microsoft SQL Server 2008 [英] Connecting android app to Microsoft SQL server 2008
本文介绍了将android应用程序连接到Microsoft SQL Server 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
首先。我是Android应用开发环境的新手,我正在尝试使用jtds-1.3.1.jar驱动程序连接到mssqlserver 2008与简单的android应用程序。我在互联网上搜了很多例子,但是我无法连接数据库。
我得到的异常是网络错误IOException:连接超时
我不知道是什么错误与我的代码我正在使用Eclipse juno IDE。
这里是我的代码
First of all. I am new to android application development environment and I am trying to connect to mssqlserver 2008 with simple android application using jtds-1.3.1.jar driver . I googled many example on internet but i failed to connect with database .
The exception i am getting is Network error IOException: connection time out
I don't know what is wrong with my code i am using Eclipse juno IDE.
here is my code
package com.example.Testproject1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.widget.TextView;
import java.sql.*;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectTodatabase();
}
public void connectTodatabase()
{
TextView txtView=(TextView)findViewById(R.id.textView2);
String url = "jdbc:jtds:sqlserver://XXX.XXX.X.XXX:1433;DatabaseName=VautomateuShoppi";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "VShopping_User";
String password = "VShopping_Pass";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
// Establish the connection.
Class.forName(driver);
con = DriverManager.getConnection(url, userName, password);
// Create and execute an SQL statement that returns some data.
String SQL = "select * from SeoMaster";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
txtView.setText(rs.getString(2));
}
}
catch(Exception ex)
{
txtView.setText(ex.getMessage().toString());
}
}
}
推荐答案
添加这些代码
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
这篇关于将android应用程序连接到Microsoft SQL Server 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文