Guest User

Untitled

a guest
Feb 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. public class SqlServerConnection {
  2.  
  3. @SuppressLint("NewApi")
  4. public Connection SqlConnection(String server,String database, String user, String password)
  5. {
  6. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  7. StrictMode.setThreadPolicy(policy);
  8. Connection connection = null;
  9. String ConnectionURL = null;
  10.  
  11. try{
  12. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  13. ConnectionURL = "jdbc:jtds:sqlserver://" + server + ":1433/" + database + ";user=" + user+ ";password=" + password + ";";
  14. connection = DriverManager.getConnection(ConnectionURL);
  15. }
  16. catch (SQLException se){Log.e("error here 1 : ", se.getMessage());}
  17. catch (ClassNotFoundException e){Log.e("error here 2 : ", e.getMessage());}
  18. catch (Exception e){Log.e("error here 3 : ", e.getMessage());}
  19.  
  20. return connection;
  21. }
  22.  
  23. public class Search extends AsyncTask<String,String,String> {
  24.  
  25. // Declaring connection variables
  26. Connection con;
  27. public static String ScannerResult;
  28. public static String z = "";
  29. public static Boolean isSuccess = false;
  30.  
  31.  
  32.  
  33. DataConnection dataConnection = new DataConnection();
  34.  
  35. @Override
  36. protected String doInBackground(String... params) {
  37.  
  38. if (ScannerResult.trim().equals(""))
  39. z = "Please enter User Id and Password";
  40. else {
  41. con = dataConnection.SqlConnection("mySqlServer","mydb" ,"myUser" , "Password");
  42. try {
  43.  
  44. if (con == null) {
  45. z = "Error in connection with SQL server";
  46. } else {
  47. String query = "SELECT * FROM [EmpDevicesList] WHERE Record='" + ScannerResult + "';";
  48. Statement stmt = con.createStatement();
  49. ResultSet rs = stmt.executeQuery(query);
  50.  
  51. if (rs.next()) {
  52. Device.setDeviceTag(rs.getString("Record"));
  53. Device.setDeviceType(rs.getString("Type"));
  54. Device.setDeviceBrand(rs.getString("Brand"));
  55. Device.setDeviceModel(rs.getString("Model"));
  56. isSuccess = true;
  57. } else {
  58. z = "Your search - " + ScannerResult + " - did not match any record.n"
  59. isSuccess = false;
  60. }
  61. }
  62. } catch (Exception ex) {
  63. isSuccess = false;
  64. z = ex.getMessage();
  65. }
  66. }
  67. return z;
  68. }
  69.  
  70. }
  71.  
  72. public class ScanActivity extends AppCompatActivity implements
  73. ZXingScannerView.ResultHandler {
  74.  
  75.  
  76. MediaTools mediaTools = new MediaTools();
  77. private ZXingScannerView scannerView;
  78. Search search = new Search();
  79.  
  80. @Override
  81. public void onCreate(Bundle state) {
  82. super.onCreate(state);
  83. scannerView = new ZXingScannerView(this);
  84. setContentView(scannerView);
  85. }
  86.  
  87. @Override
  88. public void onResume() {
  89. super.onResume();
  90. scannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
  91. scannerView.startCamera(); // Start camera on resume
  92. scannerView.setAutoFocus(true);
  93. }
  94.  
  95. @Override
  96. public void onPause() {
  97. super.onPause();
  98. scannerView.stopCamera(); // Stop camera on pause
  99.  
  100.  
  101. }
  102.  
  103. @Override
  104. public void handleResult(Result rawResult) {
  105. SearchLib.ScannerResult = rawResult.getText();
  106. searchLib.execute("");
  107. mediaTools.Beep();
  108.  
  109. String s1 = Device.getDeviceTag();
  110. String s2 = Device.getDeviceType();
  111. String s3 = Device.getDeviceBrand();
  112. String s4 = Device.getDeviceModel();
  113. Toast.makeText(getBaseContext(),
  114. "Tag : " + s1
  115. + "nType : " + s2
  116. + "nBrand : " + s3
  117. + "nModel : " + s4,
  118. Toast.LENGTH_LONG).show();
  119.  
  120.  
  121.  
  122. finish();
  123. }
  124.  
  125.  
  126. }
Add Comment
Please, Sign In to add comment