显示网络未连接之后退出程序
public class connect {
public static Connection getConnection(String dbName) throws SQLException {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动
String ip = "rm-bp1t9js01f765a35k2o.mysql.rds.aliyuncs.com";
conn =(Connection) DriverManager.getConnection(
"jdbc:mysql://" + ip + ":3306/" + dbName,
"zp_x", "Zpx008102");
MainActivity.conn_on=1;//用于向主函数传参,判断连接是否成功
}catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
MainActivity.conn_on=2;//用于向主函数传参,判断连接是否成功
}
return conn;//返回Connection型变量conn用于后续连接
}
final EditText username = (EditText) findViewById(R.id.IDinput);//取得输入框的对象
final EditText password = (EditText) findViewById(R.id.code_input);
final TextView conn = (TextView) findViewById(R.id.conn);//取得网络提示框的对象
conn.setBackgroundColor(Color.RED);//默认设成红色
final Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
switch (conn_on)//根据返回值判断网络连接是否成功
{
case 1:conn.setText("网络连接成功");conn.setBackgroundColor(Color.GREEN);break;
case 2:conn.setText("网络连接失败");break;
}
return false;
}
});
String ip = "rm-bp1t9js01f765a35k2o.mysql.rds.aliyuncs.com"
改为
String ip = "jdbc:mysql://rm-bp1t9js01f765a35k2o.mysql.rds.aliyuncs.com?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false"
不能盲猜呀,你要去调试,或者看下logcat给你的错误提示。