How Java connects to SQL Server and implements query, modify, and Delete methods

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.