weixin_44476255
阿万音铃羽
2019-05-08 16:56

Android连接SQLserver出现问题

  • android-studio
  • sql
  • 开发语言

我根据论坛的一个帖子的代码来改的注册界面,但是当传值时系统就崩溃了
显示No message resource found for message property prop.servertype

这是DButil的代码

public class DBUtil {
private Connection connection;

public static Connection getSQLConnection(String ip, String user, String pwd, String db) {
    Connection con = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:jtds:sqlserver://" + ip + ":1433/" + db, user, pwd);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return con;
}

public static String QuerySQL(String UserName, String Name, String PassWord, String Email) {
    String result = "";
    try {

        Connection connection = DBUtil.getSQLConnection("94.191.104.***", "sa", "123456", "txl");
        String sql = "insert into tuser values(" + UserName + "," + Name + "," + PassWord + "," + Email + ");";
        Statement stmt = connection.createStatement();//
        PreparedStatement stat = connection.prepareStatement(sql);
        ResultSet rs = stat.executeQuery();//执行查询语句
        while (rs.next())//判断是否查询出数据
        {
            result= "1" ;
        }
        stmt.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
        result += "查询数据异常!" + e.getMessage();
    }
    return result;
}

public void setConnection(Connection connection) {
    this.connection = connection;
}

}

这是注册界面的代码

public class SignActivity extends Activity {

private TextView aUseName;
private TextView aName;
private TextView aPassWord;
private TextView aRpassWord;
private TextView aEmail;
private Button bSign;
private Handler handler;
boolean isTip1=false,isTip2=false,isTip3=false,isTip4=false;



@Override
protected void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign);
    intView();
    initListener();
    WorkThread wt=new WorkThread();
    wt.start();
}

private void initListener() {
    bSign.setOnClickListener(new View.OnClickListener() {
        private Editable text;

        @Override
        public void onClick(View view) {
            String UserName = aUseName.getText().toString();
            String Name = aName.getText().toString();
            String PassWord = aPassWord.getText().toString();
            String RpassWord = aRpassWord.getText().toString();
            String Email = aEmail.getText().toString();

            if (TextUtils.isEmpty(UserName)) {//用户名为空
                aUseName.setError("用户名不能为空");
                return;
            }

            //限制用户名长度在3-10位
            if(UserName.length()>10||UserName.length()<3){
                aUseName.setError("请输入3-10位数用户名");
                return;
            }else {
                isTip1=true;
            }

            //限制输入密码长度在6-10位
            if(PassWord.length()>10||PassWord.length()<6){
                aPassWord.setError("请输入6-10位数密码");
                return;
            }else {
                isTip2=true;
            }

            //限制昵称长度
            if(Name.length()>7){
                aPassWord.setError("请输入7位数以内昵称");
                return;
            }

            //判断确认密码是否和密码相等
            if (PassWord.equals(RpassWord)) {//确认密码和密码相等
                isTip3=true;
            }else {                         //确认密码和密码不相等
                aRpassWord.setError("两次输入密码不相等");
                return;
            }

            //判断电子邮箱是否为空
            if (TextUtils.isEmpty(Email)) {//电子邮箱为空
                aEmail.setError("电子邮箱不能为空");
                return;
            }

            //判断电子邮箱格式
            if(Email.matches("[a-zA-Z0-9._-]+@[a-z0-9]+.[a-z]+")){
                isTip4=true;
            }else{
                aEmail.setError("电子邮箱格式不正确");
                return;
            }

            if(isTip1==true&&isTip2==true&&isTip3==true&&isTip4==true) {
                Message m = handler.obtainMessage();//获取事件
                Bundle b = new Bundle();
                b.putString("Usename", UserName);
                b.putString("Name", Name);//以键值对形式放进 Bundle中
                b.putString("Password", PassWord);
                b.putString("Email", Email);
                m.setData(b);
                m.what = 0;
                handler.sendMessage(m);


            }
        }
    });
}
class WorkThread extends  Thread{
    @Override
    public  void run(){
        Looper.prepare();
         handler = new Handler() {
            @Override
            public void handleMessage(Message m) {
                super.handleMessage(m);
                Bundle b = m.getData();//得到与信息对用的Bundle
                String Usename = b.getString("Usename");//根据键取值
                String Name = b.getString("Name");
                String Password = b.getString("Password");//根据键取值
                String Email = b.getString("Email");
                DBUtil db = new DBUtil();//调用数据库查询类
                String ret = db.QuerySQL(Usename,Name,Password,Email);//得到返回值
                if (ret.equals("1"))//为1,页面跳转,登陆成功
                {
                    Toast.makeText(SignActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(SignActivity.this, "注册失败", Toast.LENGTH_SHORT).show();

            }
        };
        Looper.loop();//Looper循环,通道中有数据执行,无数据堵塞
    }
}


private void intView(){
    aUseName =(TextView)findViewById(R.id.ausename);
    aName=(TextView)findViewById(R.id.aname);
    aPassWord=(TextView)findViewById(R.id.apassword);
    aRpassWord=(TextView)findViewById(R.id.arpassword);
    aEmail=(TextView)findViewById(R.id.aemail);
    bSign= findViewById(R.id.bsign);

}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐

换一换