Example connect to google cloud sql database server with android

Download library jtds-1.3.1.jar. and add to your project.
Connect to database source code:
package com.techmonk.loginuikit;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** * Created by Chinh 25/10/2018. */ public class ConnectSQL {
String ip,db,DBUserNameStr,DBPasswordStr;
@SuppressLint("NewApi")
public Connection connectionclasss()
{
// Declaring Server ip, username, database name and password ip = "35.198.225.205";
db = "JLPT";
DBUserNameStr = "chinh";
DBPasswordStr = "123";
// Declaring Server ip, username, database name and password StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
java.sql.Connection connection = null;
String ConnectionURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + ip +";databaseName="+
db + ";user=" + DBUserNameStr+ ";password=" + DBPasswordStr + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return connection;
}
}
Get data source code:
package com.techmonk.loginuikit;
import android.support.v4.app.NotificationCompatSideChannelService;
import com.techmonk.loginuikit.ConnectSQL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * Created by Chinh on 25/10/2018 */public class Get_Data {
Connection connect;
String ConnectionResult = "";
Boolean isSuccess = false;
String query;
public List<Map<String,String>> Get_Data(String QuestionID, String doAct, String Jlevel, String JSelPG ) {
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
try {
ConnectSQL conStr=new ConnectSQL();
connect =conStr.connectionclasss(); // Connect to database if (connect == null)
{
ConnectionResult = "Check Your Internet Access!";
}
else {
// Change below query according to your own database. int var3 = doAct.compareTo(">");
if (var3 == 0) {
query = "select * from JQuestion where QuestionID "+doAct+" '"+QuestionID+" ' " +
"and Level = '"+Jlevel+" ' and JselPG = '"+JSelPG+" ' order by QuestionID Asc";
}
else {
query = "select * from JQuestion where QuestionID "+doAct+" '"+QuestionID+" '" +
" and Level = '"+Jlevel+" ' and JselPG = '"+JSelPG+" ' order by QuestionID Desc";
}
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
Map<String,String> datanum=new HashMap<String,String>();
datanum.put("QuestionID",rs.getString("QuestionID"));
datanum.put("QContent",rs.getString("QContent"));
datanum.put("Level",rs.getString("Level"));
datanum.put("Answer1",rs.getString("Answer1"));
datanum.put("Answer2",rs.getString("Answer2"));
datanum.put("Answer3",rs.getString("Answer3"));
datanum.put("Answer4",rs.getString("Answer4"));
datanum.put("Correct",rs.getString("Correct"));
data.add(datanum);
return data;
}
ConnectionResult = " successful";
isSuccess=true;
connect.close();
}
}
catch (Exception ex)
{
isSuccess = false;
ConnectionResult = ex.getMessage();
}
return data;
}
public List<Map<String,String>> Get_Count_Data( String Jlevel ) {
String A = " ";
String B= " ";
String C= " ";
String D= " ";
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
try {
ConnectSQL conStr=new ConnectSQL();
connect =conStr.connectionclasss(); // Connect to database if (connect == null)
{
ConnectionResult = "Check Your Internet Access!";
}
else {
query = "select count(JselPG) AS SumSel from JQuestion where Level = '"+Jlevel+" ' and JselPG = 'A' ";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
A = rs.getString("SumSel").toString();
}
// query = "select count(JselPG) AS SumSel from JQuestion where Level = '"+Jlevel+" ' and JselPG = 'B' ";
stmt = connect.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
B = rs.getString("SumSel").toString();
}
// query = "select count(JselPG) AS SumSel from JQuestion where Level = '"+Jlevel+" ' and JselPG = 'C' ";
stmt = connect.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
C = rs.getString("SumSel").toString();
}
// query = "select count(JselPG) AS SumSel from JQuestion where Level = '"+Jlevel+" ' and JselPG = 'D' ";
stmt = connect.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
D = rs.getString("SumSel").toString();
}
Map<String,String> datanum=new HashMap<String,String>();
datanum.put("A",A);
datanum.put("B",B);
datanum.put("C",C);
datanum.put("D",D);
data.add(datanum);
ConnectionResult = " successful";
isSuccess=true;
connect.close();
}
}
catch (Exception ex)
{
isSuccess = false;
ConnectionResult = ex.getMessage();
}
return data;
}
}


No comments

Powered by Blogger.