Estou tentando fazer conexão direta com o SQL Server, para testes, mas na hora do teste de "Cadastrar" clicando no botão, o app simplesmente trava, há algo de errado no código?
package br.com.agemstar.testesql;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
EditText edtnome, edtend;
Button carregar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtnome = (EditText)findViewById(R.id.edtNome);
edtend = (EditText)findViewById(R.id.edtEnd);
carregar = (Button) findViewById(R.id.btnN1);
carregar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
cadastrarUsuario();
}
});
}
public Connection conexaoBD(){
Connection connection = null;
try{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.0.169;databaseName=ANDROID_SQL;user=sa;password=@dm1n102030;");
}catch (Exception e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
return connection;
}
public void cadastrarUsuario(){
try{
PreparedStatement pst = conexaoBD().prepareStatement("insert into usuarios values (?,?)");
pst.setString(1,edtnome.getText().toString());
pst.setString(2,edtend.getText().toString());
pst.executeUpdate();
Toast.makeText(getApplicationContext(),"REGISTRO CONCLUIDO",Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
}