从Android Studio连接到SQL Server

时间:2018-07-13 01:44:41

标签: sql-server android-studio

我正在创建一个android应用程序简单注册表格,并且正在使用具有SQL Server离线功能的Android Studio。

我的问题是当我填写注册表时,总是出现错误:

  

与SQL Server有关的错误

我的代码有什么问题?有人可以帮我吗?

我使用的是wifi连接而不是以太网连接,并且我关闭了防火墙。

代码

Connection con = connectionClass.CONN();

if (con == null) {
    message = "Error in connection with SQL server";
} else {
    String query = "select * from TBL_USER where username='" + userid + "'";

    Statement stmt = con.createStatement();

    ResultSet rs = stmt.executeQuery(query);

    if(rs.next())
    {
        message = "Username "+ userid +" Already Exists!";
        isSuccess = false;
    }
    else
    {
        String query1 = "insert into TBL_USER (username, password, firstname, lastname) values('"+userid+"','"+pw+"','"+firstname+"','"+lastname+"')";
        PreparedStatement stmt1 = con.prepareStatement(query1);
        stmt1.executeUpdate();

        message = "Sign Up Successfull " + firstname + " "  + lastname;
        isSuccess = true;
    }
}

ConnectionClass

public class ConnectionClass {

String ip = "192.168.137.147:1433";
String db = "RegistrationForm";
String un = "admin";
String password = "sa";

@SuppressLint("NewApi")
public Connection CONN() {
    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");
        ConnURL = "jdbc:jtds:sqlserver://" + ip + "/" + db+ ";user=" + un + ";password=" + password + ";";

        conn = DriverManager.getConnection(ConnURL);

    }catch (SQLException se)
    {
        Log.e("error here 1 : ", se.getMessage());
    }
    catch (ClassNotFoundException e)
    {
        Log.e("error here 2 : ", e.getMessage());
    }
    catch (Exception e)
    {
        Log.e("error here 3 : ", e.getMessage());
    }

    return conn;
}
}

0 个答案:

没有答案