Sql server 从Android Studio连接到SQL Server
我正在创建一个android应用程序简单注册表,我正在使用android Studio和SQL Server脱机 我的问题是,当我填写注册表时,总是会出现错误: 与SQL Server连接时出错 我的代码怎么了?有人能帮我吗 我使用的是wifi连接而不是以太网连接,我关闭了防火墙 代码Sql server 从Android Studio连接到SQL Server,sql-server,android-studio,Sql Server,Android Studio,我正在创建一个android应用程序简单注册表,我正在使用android Studio和SQL Server脱机 我的问题是,当我填写注册表时,总是会出现错误: 与SQL Server连接时出错 我的代码怎么了?有人能帮我吗 我使用的是wifi连接而不是以太网连接,我关闭了防火墙 代码 Connection con = connectionClass.CONN(); if (con == null) { message = "Error in connection with SQL s
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;
}
}
连接类:
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;
}
}
-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-签出