记录未使用Android和Sqlserver添加到Sqlserver数据库中

时间:2019-07-19 11:14:44

标签: android

我正在使用android和sqlserver数据库创建简单的Crud。当我单击添加按钮记录未添加到数据库中。但是没有显示错误, 我在下面尝试的代码附加了代码。有人帮助我解决了这个问题。我两天前都在练习。但是我不能

 EditText ed1,ed2;
    Button b1,b2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = findViewById(R.id.title);
        ed2 = findViewById(R.id.course);
        b1 = findViewById(R.id.btn1);
        b2 = findViewById(R.id.v1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
    }

 public void insert() {
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            String connectionUrl = "jdbc:jtds:sqlserver://KOBINATH-PC; databaseName=skill; user=sa; password=admin123";
            Connection con = DriverManager.getConnection(connectionUrl);
            PreparedStatement pst;
            String title = ed1.getText().toString();
            String description = ed2.getText().toString();

            pst = con.prepareStatement("insert into course(coursename,fee)values(?,?)");
            pst.setString(1, title);
            pst.setString(2, description);

            pst.executeUpdate();

            Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();

            ed1.setText("");
            ed2.setText("");
            ed1.requestFocus();
        } catch (Exception ex) {
            Toast.makeText(this, "Record Fail", Toast.LENGTH_LONG).show();
        }
    }

0 个答案:

没有答案