Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SqlServerConnection {
- @SuppressLint("NewApi")
- public Connection SqlConnection(String server,String database, String user, String password)
- {
- 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 + ":1433/" + database + ";user=" + user+ ";password=" + password + ";";
- 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;
- }
- public class Search extends AsyncTask<String,String,String> {
- // Declaring connection variables
- Connection con;
- public static String ScannerResult;
- public static String z = "";
- public static Boolean isSuccess = false;
- DataConnection dataConnection = new DataConnection();
- @Override
- protected String doInBackground(String... params) {
- if (ScannerResult.trim().equals(""))
- z = "Please enter User Id and Password";
- else {
- con = dataConnection.SqlConnection("mySqlServer","mydb" ,"myUser" , "Password");
- try {
- if (con == null) {
- z = "Error in connection with SQL server";
- } else {
- String query = "SELECT * FROM [EmpDevicesList] WHERE Record='" + ScannerResult + "';";
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- if (rs.next()) {
- Device.setDeviceTag(rs.getString("Record"));
- Device.setDeviceType(rs.getString("Type"));
- Device.setDeviceBrand(rs.getString("Brand"));
- Device.setDeviceModel(rs.getString("Model"));
- isSuccess = true;
- } else {
- z = "Your search - " + ScannerResult + " - did not match any record.n"
- isSuccess = false;
- }
- }
- } catch (Exception ex) {
- isSuccess = false;
- z = ex.getMessage();
- }
- }
- return z;
- }
- }
- public class ScanActivity extends AppCompatActivity implements
- ZXingScannerView.ResultHandler {
- MediaTools mediaTools = new MediaTools();
- private ZXingScannerView scannerView;
- Search search = new Search();
- @Override
- public void onCreate(Bundle state) {
- super.onCreate(state);
- scannerView = new ZXingScannerView(this);
- setContentView(scannerView);
- }
- @Override
- public void onResume() {
- super.onResume();
- scannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
- scannerView.startCamera(); // Start camera on resume
- scannerView.setAutoFocus(true);
- }
- @Override
- public void onPause() {
- super.onPause();
- scannerView.stopCamera(); // Stop camera on pause
- }
- @Override
- public void handleResult(Result rawResult) {
- SearchLib.ScannerResult = rawResult.getText();
- searchLib.execute("");
- mediaTools.Beep();
- String s1 = Device.getDeviceTag();
- String s2 = Device.getDeviceType();
- String s3 = Device.getDeviceBrand();
- String s4 = Device.getDeviceModel();
- Toast.makeText(getBaseContext(),
- "Tag : " + s1
- + "nType : " + s2
- + "nBrand : " + s3
- + "nModel : " + s4,
- Toast.LENGTH_LONG).show();
- finish();
- }
- }
Add Comment
Please, Sign In to add comment