SQL Server Connection

Asked

Viewed 266 times

0

I’m trying to make direct connection to SQL Server, for testing, but at the time of testing "Register" by clicking the button, the app simply hangs, there’s something wrong in the code?

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();
        }
    }
}
  • 1

    Does the 'users' table have only name and address? If there are other values, it is necessary to declare in which fields you will insert: INSERT INTO USUARIOS (TXT_NOME, TXT_ENDERECO) VALUES (?, ?)

  • Hangs, but shows error message, which?

  • Probably that android does not allow direct connection to Database Servers. As you can see in that reply. now if you really want to connect directly to the bank You got that other answer

  • @John, the app just stops responding and I have to close it.

  • @Csorgo, I’ll take this test, and I’ll let you know.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.