gpt4 book ai didi

java.sql.SQLException : Network error IOException: failed to connect ETIMEDOUT 异常

转载 作者:太空狗 更新时间:2023-10-29 14:43:55 28 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,我必须将 android 应用程序与 SQL 服务器数据库连接起来,以便使用用户名和密码对用户进行身份验证。我使用了 jtds 库来提供与 android 的 Sql 连接。下面是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText username,password;
CheckBox mCbShowPwd;
Connection con;
String un,pass,db,ip,in;
TextView t;

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

username = (EditText) findViewById(R.id.username_edtext);
password = (EditText) findViewById(R.id.passwd_edtext);
mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
t = (TextView) findViewById(R.id.text);
final Button forget_btn = (Button) findViewById(R.id.forget_btn);
forget_btn.setOnClickListener(this);
final Button login_btn = (Button) findViewById(R.id.login_button);
login_btn.setOnClickListener(this);
ip = "172.16.0.**";
in="********";
db = "*******";
un = "****";
pass = "****";
mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());}
}
});
}

@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.forget_btn:
{
Intent intent=new Intent("com.example.pc.uowv1.ForgetPassActivity");
startActivity(intent);
break;
}
case R.id.login_button:
{
CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
checkLogin.execute("");
break;
}
}
}
public class CheckLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;
String usernam = username.getText().toString();
String passwordd = password.getText().toString();

@Override
protected void onPreExecute()
{
//progressBar.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(String r)
{
Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess)
{
Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_SHORT).show();
}
}
@Override
protected String doInBackground(String... params)
{
if(usernam.trim().equals("")|| passwordd.trim().equals(""))
z = "Please enter Username and Password";
else
{
try
{
con = connectionclass(un, pass, db, ip,in);
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
String query = "select * from Login where username= '" + usernam.toString() +"'and password='"+passwordd.toString()+"'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
//mApp.setmGuser(usernam);
z = "Login successful";
isSuccess=true;
con.close();
Intent intent=new Intent(MainActivity.this,MAin2Activity.class);
intent.putExtra("username",usernam.toString());
startActivity(intent);
}
else
{
z = "Invalid Username and password!";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = ex.getMessage();
}
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server,String instance)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try
{

Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + "/"+ database+";instance=" + instance + ";user=" + user+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
t.setText(se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
t.setText(e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
t.setText(e.getMessage());
}
return connection;
}
}

代码在同一个网络上工作,但是当我连接到我的 android 设备中的其他网络时,它会抛出

Exception E/error here 1 :: Network error IOException: failed to 
connect to /172.16.0.** (port 1433): connect failed: ETIMEDOUT
(Connection timed out).

最佳答案

恕我直言,您选择了错误的方式。不要使用从 Android 到 SQL 服务器的连接。有必要在 SQL 服务器端创建一个 Web 服务,并从 Android 使用该 Web 服务。 JDBC 驱动程序不适用于移动网络。

关于java.sql.SQLException : Network error IOException: failed to connect ETIMEDOUT 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43061357/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com