JDBC "native mode" of Java Operational Database __c#

Source: Internet
Author: User
Tags reflection sql injection stmt

Data download: With the back of the dbutils packaging, there is a need to download it. Download URL: http://download.csdn.net/detail/xiaozhegaa/9816808

Introduction: Java has many kinds of techniques for database operation. For example, say Jdbc,dbutil +c3p0,hibernate,jdbctemplate and so on "These five will sum up", in the following words use some advanced framework to operate the database, such as Hibernate. But the underlying operation of the database is important, the high-level framework is also based on the foundation, so here or summary of some common database operation technology, interested to look at. Or just pick the part that interests you. See
One: jdbc what is jdbc. Why use JDBC. How do I use JDBC? "What +why + how
What" the rules provided by Sun to enable Java programs to flexibly access a variety of relational databases. "Why" means that the Java application is not required to manipulate the database directly, but rather to operate the database uniformly through the methods provided by sum, then we do not need to write different code for different databases. Only need to configure in the configuration file, write code more convenient. Allow Java programs to be portable "how" to say "The following cases are all in MySQL for example"

Two: Development step
1) Import MySQL package, this step is necessary, regardless of the technical operation of the database, you need to import MySQL package

2 Six fixed steps of JDBC
1, register database driver [use reflection]
2, Get database Connection object Connection
3, create SQL Object
4, Execute SQL command, return result set
5, process result set
6, turn off knot Fruit Set

1, there are two ways to register database driver
    : Direct Registration Database driver
        drivermanager.registerdriver (new Driver ());
    The second is: the use of reflection mechanism to indirectly load the database drive, recommended the second
        class.forname ("Com.mysql.jdbc.Driver");
2. Get the database Connection object connection
        //Get the bridge with MySQL database connection, the parameters are: Connect database  username Password Connection conn = Drivermanager.getconnection (                "jdbc:mysql:///zz2017", "root", "Xiaozheng");
The first two steps need to be remembered below. Below

//4: Execute SQL statement
insert/update/delete----preparedstatement. executeupdate (SQL): The return value represents the number
   of rows that affect the record Select------------------preparedstatement. Exeuctequery (): Returns a value that represents a qualifying record   

"Note" When creating SQL objects, someone uses statement, and my case uses PreparedStatement. The advantage of using this is that the variable can be represented as a placeholder, and it can prevent SQL injection, so I take this approach without statement

Case Source String Driver = "Com.mysql.jdbc.Driver";
        String url = "JDBC:MYSQL://127.0.0.1:3306/MYDB2";
        String user = "root";
        String password = "root";
        String sql = "INSERT into user (Name,gender,salary) VALUES (?,?,?)";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
            try {//Register database driver Class.forName (driver);
            Get database Connection conn = drivermanager.getconnection (URL, user, password);
            To precompile, where parameters are set pstmt = conn.preparestatement (sql);
            Pstmt.setstring (1, "Xiaozheng");
            Pstmt.setstring (2, "male");
            Pstmt.setfloat (3,8000);
            To compile rs = Pstmt.executequery ();
                while (Rs.next ()) {int id = rs.getint ("id");
                String name = rs.getstring ("name");
                String gender = rs.getstring ("gender");
     float salary = rs.getfloat ("salary");           System.out.println (id + ":" + name + ":" + Gender + ":" + salary);
        } catch (Exception e) {e.printstacktrace ();
                finally {if (rs!=null) {//lightweight, the time and resources required to create and destroy Rs are smaller try {rs.close ();
                catch (Exception e) {e.printstacktrace (); } if (Stmt!=null) {//lightweight, the time and resources required to create and destroy Rs are smaller try {stmt.close ()
                ;
                catch (Exception e) {e.printstacktrace (); } if (Conn!=null) {//Heavyweight, the time and resources required to create and destroy Rs are smaller try {conn.close ()
                ;
                catch (Exception e) {e.printstacktrace (); }
            }

To remind you that there are a lot of the same places in the code above, too long. Then you can extract the same part into a tool class, which is demonstrated in the form of a tool class. Tool class will be packaged for everyone, there is a need to download and see the


Three: Curl action
Insert operation diagram is a bit unclear, suggest you can drag to a new window to open a picture to see "

Read operation

Delete

Modify action

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.