为什么jdbc在执行查询时第一次返回null?

时间:2018-02-21 09:27:44

标签: android

以下代码正在使用sql-server db& ZXingScannerView.ResultHandler条形码扫描仪。

当我第一次扫描条形码时,我从执行的数据库查询中获取null,我必须扫描条形码两次以获得结果。我检查了条形码扫描器库,一切正常,与jdbc相同。

SqlServerConnection.java的代码

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;
}

}

Search.java 的代码:

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;

    SqlServerConnection sqlsrverconnection = new SqlServerConnection ();

    @Override
    protected String doInBackground(String... params) {

        if (ScannerResult.trim().equals(""))
            z = "Please enter User Id and Password";
        else {
            con = sqlsrverconnection.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;
    }

}

ScannActivity.java的代码

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) {
        Search.ScannerResult = rawResult.getText();
        search.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();
    }
}

表架构

Record | Type  | Brand | Modle
--------------------------------
 1002  |  PC   |  HP   |  X-1
 1003  |  PC   |  Dell |  788

0 个答案:

没有答案