Answer the question
In order to leave comments, you need to log in
Android Studio connecting to MS SQL 2016 DB?
Hello!
I am trying to insert data into a MS SQL DB table via android, there is this code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onClick_Info(View v)
{
Intent intent = new Intent(this, ActivityInfo.class);
startActivity(intent);
}
protected void onClick_Store(View v)
{
String instanceName = "192.168.1.100\\SQLEXPRESS";
String db = "Computer";
String username = "Admin";
String password = "Admin";
String connectionUrl = "jdbc:sqlserver://%1$s;databaseName=%2$s;user=%3$s;password=%4$s;";
String connectionString = String.format(connectionUrl, instanceName, db, username, password);
try {
Log.w("Run: ", "Connection open!");
Connection con = DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement();
stmt.execute("INSERT INTO Product(maker, model, type) VALUES ('D', 2305, 'PC')");
stmt.close();
con.close();
Log.w("Run: ", "Connection close!");
}
catch (SQLException ex)
{
Log.w("SQLException error: ", ex.getMessage());
}
catch (Exception ex)
{
Log.w("Exception error: ", ex.getMessage());
}
}
}
Answer the question
In order to leave comments, you need to log in
Problem solved!
Like this:
In the project structure, I added a module to the project itself (I can’t explain in more detail because I don’t understand how it works myself)
Hello, could you tell me materials on connection, interaction with MSSQL.... or write where I can talk to you in a lik?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question