IatroJordan
Lurker
I have been trying to connect
my application
through a jdbc connection to my sql database. I understand that android 3.0 above, connection cannot be done on the UI itself and it is required to be run at the background. I used a thread to connect to the database and send information to the database before running the next activity. I have been receiving
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed"
Can anyone advice me on how to use a connect my application
?
Mainactivity
public void onFinish() {
try {
Thread thread = new Thread(new Runnable(){
String pId = "1",gId="40";
int sNo=1,finalScore = 20,time = 40;
@override
public void run() {
GameManager gameManager = new GameManager(pId,gId,sNo,finalScore,time);
gameManager.GameMgrStart();
}
});
thread start
();
FileOutputStream writer = openFileOutput("logs.txt", MODE_APPEND);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(writer));
out.write("Total touch on the screen: " + counterTouch);
out.newLine();
out.write("Total touch on the red circle: " + redTouch);
out.newLine();
out.write("Total Score: " + score);
out close
();
} catch (Throwable t){
}
Intent intent = new Intent(GameActivity.this,EndActivity.class);
intent.putExtra("score",tempScore);
intent.putExtra("intCounter",counterTouch);
startActivity(intent);
}
Gamemanager application:
public class GameManager {
/*
//database connectors (local)
String dbURL = "jdbc:sqlserver://localhost:1433;";
String dbName = "databaseName=*****;";
String dbSecurity = "integratedSecurity=true;";
*/
//database connectors (server)
String dbURL = "jdbc:sqlserver://******";
String dbName = "databaseName=*****;";
String dbUsername = "user=****;";
String dbPassword = "password=*****;";
String patientID;
String gameID;
int sqlNo;
int score;
int time;
public GameManager(String pID, String gID, int sNo){
patientID=pID;
gameID = gID;
sqlNo = sNo;
}
public GameManager(String pID, String gID, int sNo, int s, int t){
patientID=pID;
gameID = gID;
sqlNo = sNo;
score = s;
time = t;
}
//This method is called to run Game
Manager
public void GameMgrStart(){
Connection
conn=null;
String sqlStatement;
try {
conn = dbConnect();
sqlStatement = sqlStats();
//Run statement
PreparedStatement cmdSql = conn.prepareStatement(sqlStatement);
cmdSql.execute();
System.out.println("" + sqlStatement);
}
catch (Exception e) {
e.printStackTrace();
}
}
//List of SQL Statements
public String sqlStats(){
String sqlStatement= "";
if(sqlNo == 1){
sqlStatement = "INSERT INTO GameRecord(Score, timeTaken, AssignedGameID) Values('" + score + "', '" + time + "', (SELECT top 1 assignedGameID FROM assignedGame WHERE gameID='" + gameID + "' and patientAllocationID=(SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')))";
}
return sqlStatement;
}
//Method to connect to database
public Connection dbConnect(){
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//conn = DriverManager.getConnection(dbURL+dbName+dbSecurity);
conn = DriverManager.getConnection(dbURL+dbName+dbUsername+dbPassword);
if (conn != null) {
System.out.println("Connected");
return conn;
}
else
System.out.println("Failed");
}
catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String [] args){
GameManager objGM = new GameManager("1","1",1);
objGM.GameMgrStart();
}
}
my application
through a jdbc connection to my sql database. I understand that android 3.0 above, connection cannot be done on the UI itself and it is required to be run at the background. I used a thread to connect to the database and send information to the database before running the next activity. I have been receivingcom.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed"
Can anyone advice me on how to use a connect my application
?Mainactivity
public void onFinish() {
try {
Thread thread = new Thread(new Runnable(){
String pId = "1",gId="40";
int sNo=1,finalScore = 20,time = 40;
@override
public void run() {
GameManager gameManager = new GameManager(pId,gId,sNo,finalScore,time);
gameManager.GameMgrStart();
}
});
thread start
();FileOutputStream writer = openFileOutput("logs.txt", MODE_APPEND);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(writer));
out.write("Total touch on the screen: " + counterTouch);
out.newLine();
out.write("Total touch on the red circle: " + redTouch);
out.newLine();
out.write("Total Score: " + score);
out close
();} catch (Throwable t){
}
Intent intent = new Intent(GameActivity.this,EndActivity.class);
intent.putExtra("score",tempScore);
intent.putExtra("intCounter",counterTouch);
startActivity(intent);
}
Gamemanager application:
public class GameManager {
/*
//database connectors (local)
String dbURL = "jdbc:sqlserver://localhost:1433;";
String dbName = "databaseName=*****;";
String dbSecurity = "integratedSecurity=true;";
*/
//database connectors (server)
String dbURL = "jdbc:sqlserver://******";
String dbName = "databaseName=*****;";
String dbUsername = "user=****;";
String dbPassword = "password=*****;";
String patientID;
String gameID;
int sqlNo;
int score;
int time;
public GameManager(String pID, String gID, int sNo){
patientID=pID;
gameID = gID;
sqlNo = sNo;
}
public GameManager(String pID, String gID, int sNo, int s, int t){
patientID=pID;
gameID = gID;
sqlNo = sNo;
score = s;
time = t;
}
//This method is called to run Game
Managerpublic void GameMgrStart(){
Connection
conn=null;String sqlStatement;
try {
conn = dbConnect();
sqlStatement = sqlStats();
//Run statement
PreparedStatement cmdSql = conn.prepareStatement(sqlStatement);
cmdSql.execute();
System.out.println("" + sqlStatement);
}
catch (Exception e) {
e.printStackTrace();
}
}
//List of SQL Statements
public String sqlStats(){
String sqlStatement= "";
if(sqlNo == 1){
sqlStatement = "INSERT INTO GameRecord(Score, timeTaken, AssignedGameID) Values('" + score + "', '" + time + "', (SELECT top 1 assignedGameID FROM assignedGame WHERE gameID='" + gameID + "' and patientAllocationID=(SELECT patientAllocationID FROM patientAllocation WHERE patientID='" + patientID + "')))";
}
return sqlStatement;
}
//Method to connect to database
public Connection dbConnect(){
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//conn = DriverManager.getConnection(dbURL+dbName+dbSecurity);
conn = DriverManager.getConnection(dbURL+dbName+dbUsername+dbPassword);
if (conn != null) {
System.out.println("Connected");
return conn;
}
else
System.out.println("Failed");
}
catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String [] args){
GameManager objGM = new GameManager("1","1",1);
objGM.GameMgrStart();
}
}