This blog will guide how to make a registration page in android studio and store the value in MS-SQL database by following below simple steps :
(1) Step – 1 : Create a database in SQL Server and Create a table register in it.
(2) Step – 2 : Now in Android Studio open AndroidManifest.xml and add :
<uses-permission android:name="android.permission.INTERNET"/>
(3) Step – 3 : Now create RegisterActivity and design the register page that is activity_register.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisterActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/name"
android:hint="Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/email"
android:hint="Email"
android:inputType="textEmailAddress"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/password"
android:hint="Password"
android:inputType="textPassword"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/registerbtn"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/status"
android:textSize="25dp"
android:layout_marginTop="20dp"
android:text="Register"
/>
</LinearLayout>
(4) Step – 4 : Now create a Connection Class Package to define the connection properties to SQL server that is Connection -> ConnectionClass.java
public class ConnectionClass {
public static String ip = “”;
public static String un = “”;
public static String pass = “”;
public static String db = “”;
}
(5) Step – 5 : Now write down business logic for register page that is RegisterActivity.java :
EditText name,email,password;
Button registerbtn;
TextView status;
Connection con;
Statement stmt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name = (EditText)findViewById(R.id.name);
email = (EditText)findViewById(R.id.email);
password = (EditText)findViewById(R.id.password);
registerbtn = (Button)findViewById(R.id.registerbtn);
status = (TextView)findViewById(R.id.status);
registerbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new RegisterActivity.registeruser().execute("");
}
});
}
public class registeruser extends AsyncTask<String, String , String>{
String z = "";
Boolean isSuccess = false;
@Override
protected void onPreExecute() {
status.setText("Sending Data to Database");
}
@Override
protected void onPostExecute(String s) {
status.setText("Registration Successful");
name.setText("");
email.setText("");
password.setText("");
}
@Override
protected String doInBackground(String... strings) {
try{
con = connectionClass(ConnectionClass.un.toString(),ConnectionClass.pass.toString(),ConnectionClass.db.toString(),ConnectionClass.ip.toString());
if(con == null){
z = "Check Your Internet Connection";
}
else{
String sql = "INSERT INTO register (name,email,password) VALUES ('"+name.getText()+"','"+email.getText()+"','"+password.getText()+"')";
stmt = con.createStatement();
stmt.executeUpdate(sql);
}
}catch (Exception e){
isSuccess = false;
z = e.getMessage();
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionClass(String user, String password, String database, String server){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String connectionURL = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connectionURL = "jdbc:jtds:sqlserver://" + server+"/" + database + ";user=" + user + ";password=" + password + ";";
connection = DriverManager.getConnection(connectionURL);
}catch (Exception e){
Log.e("SQL Connection Error : ", e.getMessage());
}
return connection;
}
}
(6) Step – 6 : Run the project and test it. To check the whole process go to the link below :
Dear Mr. Ketual, i have tried as same what you have done, but i have one issue when i t said registration successful the data not appear in my sql database.
LikeLiked by 1 person
Have you added sql server in your Android Studio
LikeLike
How do i add the sql server sir? I thought its already added as shown in the video. I copied all the steps and code, but came up with the same problem as the above comment
LikeLike
I have anthoder video on how to add sql server.
LikeLike