2

我正在尝试使用 jTDS-1.2.5 连接到 sql server 上的数据库。我知道这是危险和不安全的,我不应该这样做,但我只是想尝试一下,所以请幽默。为了测试 jTDS,我在 netbeans 中编写了这段代码:

package dbconnecttest;

import java.sql.*;
import net.sourceforge.jtds.jdbc.*;
import net.sourceforge.jtds.jdbcx.*;

public class DBConnectTest {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    Connection con = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();

        String connString = "jdbc:jtds:sqlserver://MY_SERVER_NAME:24923/Phone_Test;user=testLogin;password=xxxxxxxx;instance=MYINSTANCE";
        con = DriverManager.getConnection(connString,"testLogin","xxxxxxxx");
        try {
            PreparedStatement stmt = con.prepareStatement("SELECT * FROM Product_Inventory_Test WHERE ProductTest = 'Car'");
            stmt.execute();

            ResultSet rs = stmt.getResultSet();
            while (rs.next()) {
                System.out.println(rs.getString("ProductTest").replace(" ", "") + " price: $" + rs.getString("PriceTest"));
            }
            stmt.close();
        } catch (Exception e) {
            System.out.println("Statement error: " + e.getMessage());
        }
        con.close();


    }
    catch (Exception e) {
        System.out.println("Connection Error: " + e.getMessage());
    }

}
}

注意,我连接的不是 1433 端口。我在配置管理器中启用了 TCP/IP,并将所有 IP 地址的 TCP 端口设置为 24923。我还确保它没有动态分配 TCP 端口。这段代码在 netbeans 中运行良好,它从数据库中提取数据没有任何问题。我在我的android应用程序的eclipse中使用了类似的代码:

package com.xxxxxx.xxxxxx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import net.sourceforge.jtds.jdbc.*;
import net.sourceforge.jtds.jdbcx.*;


public class LoginActivity extends Activity {

TextView labelLogin;
TextView labelError;
EditText txtUsername;
EditText txtPassword;
Button btnLogin;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    //Assign properties to login form views
    labelLogin = (TextView)findViewById(R.id.labelLogin);
    labelError = (TextView)findViewById(R.id.labelError);
    txtUsername = (EditText)findViewById(R.id.txtUsername);
    txtPassword = (EditText)findViewById(R.id.txtPassword);
    btnLogin = (Button)findViewById(R.id.btnLogin);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@SuppressWarnings("deprecation")
public void login_onClick(View view) {
    Connection con = null;
    try{
        Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();



        String connString = "jdbc:jtds:sqlserver://MY_SERVER_NAME:24923/Phone_Test;user=testLogin;password=xxxxxxxx;instance=MYINSTANCE";
        String username = "testLogin";
        String password = "xxxxxxxx";
        con = DriverManager.getConnection(connString,username,password);
        PreparedStatement stmt = null;
        try {
            //Prepared statement
            stmt = con.prepareStatement("SELECT * FROM Logins WHERE Username = ? AND Password = ?");
            stmt.setString(1, txtUsername.toString());
            stmt.setString(2, txtPassword.toString());
            stmt.execute();

            ResultSet rs = stmt.getResultSet();
            if(rs.next()) {
                //Start new activity
                AlertDialog ad = new AlertDialog.Builder(this).create();
                ad.setTitle("Success!");
                ad.setMessage("You have logged in successfully!");
                ad.setButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
                ad.show();




            }
            stmt.close();
        }
        catch (Exception e) {
            stmt.close();
            AlertDialog ad = new AlertDialog.Builder(this).create();
            ad.setTitle("Error");
            ad.setMessage("Prepared statement error: " + e.getMessage());
            ad.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            });
            ad.show();
        }
        con.close();
    }
    catch (Exception e) {
        AlertDialog ad = new AlertDialog.Builder(this).create();
        ad.setTitle("Error");
        ad.setMessage("Connection error: " + e.getMessage());
        ad.setButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });
        ad.show();
    }
}

}

我已将 jar 文件添加到类路径中,一切似乎都正常。但是,当我在模拟器上运行该应用程序时,它给了我错误:

Connection Error: Unable to get information from SQL Server: MY_SERVER_NAME.

我有点坚持下一步该做什么。一切似乎都运行良好,只是无法连接。我试过去命令提示符并尝试 sqlcmd -L,它在列表中有我的服务器。我也可以从那里远程登录到我的服务器。netstat -an 显示它正在侦听端口 24923,本地 IP 地址为 0.0.0.0。我只是不确定问题出在哪里,并且我已经检查了有关解决此错误的 jTDS 常见问题解答提示。有什么想法/建议吗?

4

0 回答 0