0

我的应用程序出现问题

这是我的一段代码,我的活动抛出异常“错误连接 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());
    }
    }    
}
4

2 回答 2

0

您可以通过 Web 服务连接到 SQL Server 数据库。

这是调用 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();

确保在 Manifest.xml 文件中设置权限,

您可以在 php 或 java 中编写 webservice 来获取此值并连接到 SQL Server

于 2012-11-24T12:27:29.603 回答
0

另一种方法是使用使用三层架构的虚拟 JDBC 驱动程序:您的 JDBC 代码通过 HTTP 发送到远程 Servlet,后者在将 JDBC 代码(配置和安全性)传递给 SQL Server JDBC 驱动程序之前对其进行过滤。结果通过 HTTP 发回给您。有一些免费软件使用这种技术。这是安全的,因为数据库永远不会直接公开,您可以使用 SSL。只需谷歌“基于 HTTP 的 Android JDBC 驱动程序”。

于 2013-02-11T15:31:39.500 回答