Scenario: A: In UI Automation, when you delete data, the UI prompts "The XX is already in use and cannot be deleted." At this time, we need to do the data initialization operation, we need to delete the historical data, to ensure that the script run repeatedly, and stable nature.
B: In the new operation, you need to verify that the data exists in the background. The background data needs to be validated.
Implementation ideas:
1. Connect the database's connection address, user name, and password. Configured at the framework configuration file.
Public class Const { publicstaticfinal String db_url = "XXXX"; Public Static Final String db_databasename = "XXX"; Public Static Final String db_username = "XXX"; Public Static Final String Db_password = "XXX";}
2.DBHelper method
1 Packagecom.pensee.utils;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 ImportCom.pensee.config.Const;9 Ten Public classDBHelper { One StaticString Driver = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; A StaticString url = "jdbc:sqlserver://" + Const.db_url + ";D atabasename=" + Const.db_databasename + ""; - StaticConnection con =NULL; - StaticStatement st =NULL; the StaticResultSet res =NULL; - - - Public Static voiddataBase () { + Try { - Class.forName (driver); +con = drivermanager.getconnection (URL, "" + Const.db_username + "", "" + Const.db_password + ")); A}Catch(ClassNotFoundException e) { atSYSTEM.ERR.PRINTLN ("Load Jdbc/odbc driver failed. "); - e.printstacktrace (); -}Catch(SQLException e) { -System.err.println ("Unable to connect to database"); - e.printstacktrace (); - } in } - to /** + * Query SQL Method - * @paramSQL the * @return * * @throwsSQLException $ */Panax Notoginseng Public StaticResultSet find (String sql)throwssqlexception{//querying the database for data - //Get Connected the dataBase (); +st=con.createstatement (); A Try { theres=st.executequery (SQL); + returnRes; -}Catch(SQLException e) { $ e.printstacktrace (); $ return NULL; - } - the } - /**Wuyi * SQL Delete Modify the * @paramSQL - * @return Wu * @throwsSQLException - */ About Public Static BooleanUpdate (String SQL)throwssqlexception{//the change of additions and deletions $ //Get Connected - dataBase (); -St =con.createstatement (); - Try { A st.executeupdate (SQL); + return true; the}Catch(SQLException e) { - e.printstacktrace (); $ return false; the } the } the the}
3. How to call DB
String sql = "Update hr_staff_policy set holidaypolicy = 3 where staffno = ' 0092 '"; Boolean result = dbhelper.update (SQL); Assert.istrue (result); SYSTEM.OUT.PRINTLN ("SQL execution result is:" +dbhelper.update (SQL));
4.:
How Java connects to SQL Server and implements query, modify, and Delete methods