package com.test.data;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import com.test.serv.sqlResult;
import com.test.serv.sqlObject;
import com.test.util.utility;
public class MsSqlUtil {
private String ConnURL = "jdbc:jtds:sqlserver://strhost;databaseName=strdatabase;user=struser;password=strpassword;";
private String strError = "";
private Boolean status = false;
private Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(ConnURL);
}
catch (SQLException se) {
strError = se.getMessage();
Log.e("ERRO", se.getMessage());
}
catch (ClassNotFoundException e) {
strError = e.getMessage();
Log.e("ERRO", e.getMessage());
}
catch (Exception e) {
strError = e.getMessage();
Log.e("ERRO", e.getMessage());
}
catch(Error e2) {
strError = String.format("Parse error2:%s", e2.getMessage());
Log.e("", "Parse error2: " + e2.toString());
}
return conn;
}
public sqlObject selectDataFromSQl(String params) {
sqlObject obj = null;
try {
Connection con = CONN();
if (con == null || con.isClosed()) {
strError = "Error in connection with SQL server";
System.err.println(strError);
return null;
}
else {
String query = "SELECT * FROM dbo.tablo_adi where alanadi like N'" + params + "%'";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
obj = new sqlObject();
obj.xstring = rs.getString("stringalan");
obj.xint = rs.getInt("intalan");
obj.xdecimal = rs.getBigDecimal("decalan");
status = true;
}
if (rs != null)
rs.close();
ps.close();
con.close();
}
}
catch (Exception ex) {
strError = ex.getMessage();
ex.printStackTrace();
}
return obj;
}
public sqlResult sendSqlCommand(String... params) {
sqlResult res = new sqlResult();
try {
Connection con = CONN();
if (con == null || con.isClosed()) {
res.error = "Error in connection with SQL server";
res.statu = false;
System.err.println(strError);
return res;
}
else {
PreparedStatement ps = con.prepareStatement(params[0]);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
res.error = rs.getString("alanadi");
res.xdouble = rs.getBigDecimal("alanadi2");
res.statu = true;
}
if (rs != null)
rs.close();
ps.close();
con.close();
}
}
catch (Exception ex) {
res.error = ex.getMessage();
res.statu = false;
strError = ex.getMessage();
ex.printStackTrace();
}
return res;
}
public BigDecimal selectUretimDegeri(String... params) {
BigDecimal alanDeger = null;
try {
Connection con = CONN();
if (con == null) {
strError = "Error in connection with SQL server";
System.err.println(strError);
return null;
}
else {
String query = "SELECT * FROM dbo.fonktion_name(" + params[0] + "," + params[1] + ")";
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
alanDeger = rs.getBigDecimal("alanadi");
status = true;
}
if (rs != null)
rs.close();
ps.close();
con.close();
}
}
catch (Exception ex) {
strError = ex.getMessage();
ex.printStackTrace();
}
return alanDeger;
}
public String getError() {
return strError;
}
public Boolean getStatus() {
return status;
}
}
Kullanimina örnek olarak
MsSqlUtil mssql = new MsSqlUtil();
sqlObject selectObj = mssql.selectDataFromSQl(strSelect);
if (mssql.getStatus()) {
if (selectObj == null) {
Logger.Error("Select Obj", String.format("%d-) Data yok:%s", 1071, mssql.getError()), getLocalClassName());
PostSomethingToMainThread(mssql.getError(), false);
}
}
else {
Logger.Error("Select Obj", String.format("%d-) Data sorgulanamadi:%s", 1080, mssql.getError()), getLocalClassName());
}