我正在制作一个用户日志应用程序,在执行模拟器时我可以注册用户,验证用户在我的数据库中的库存,但是当我在手机上执行应用程序时,它没有连接到我的数据库。
这是我到数据库的连接代码:
public class ConnectionDB extends AppCompatActivity {
Connection connection;
private static String ip = "192.168.1.9"; //"192.168.33.1";
private static String port = "1433";
private static String classs = "net.sourceforge.jtds.jdbc.Driver";
private static String db = "PHOTOGO";
private static String user = "mizrahijulio";
private static String password = "julio1207";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//Connection method
public Connection connectBD(){
connection=null;
try {
//Acces Permition to DB
StrictMode.ThreadPolicy policicy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policicy);
Class.forName(classs).newInstance();
connection= DriverManager.getConnection("jdbc:jtds:sqlserver://"+ip+":"+port+";databaseName="+db+";user="+user+";password="+password+";instance=SQLEXPRESS;");
}catch(SQLException sq){
Toast.makeText(getApplicationContext(),sq.getMessage(),Toast.LENGTH_SHORT).show();
}
catch (ClassNotFoundException cfe){
Toast.makeText(getApplicationContext(),cfe.getMessage(),Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
return connection;
}
public Connection CloseConnection() throws SQLException {
try{
if(connection!=null){
connection.close();}
}
catch(SQLException sq){
Toast.makeText(getApplicationContext(),sq.getMessage(),Toast.LENGTH_SHORT).show();
}
return connection;
}
如果您的数据库是本地的,您的手机和服务器计算机必须连接到同一网络。可能这就是问题所在,如果您的手机连接到 SIM 网络或任何其他与服务器计算机网络不同的网络,并且您尝试访问它,则不会。在谈论您的模拟器时,它显然已连接到服务器网络,因此它可以工作。
试试吧,可能是这个问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句