[Javaweb Basic] 003.JAVA access MySQL Database

Source: Internet
Author: User

The above two explains the simple JSP + servlet build and request, then we must use the data interaction , that is, manipulate the database data , including the number of the increase, delete, modify, query . We're going to use simple MySql as an example.

We need to introduce the driver package Mysql-connector-java.jar, to go online to download, there are many.

Now I'll follow the code to see how to make an additional check.

1. Open the database

    Driver name    Private String driver   = "Com.mysql.jdbc.Driver";    The URL points to the database name you want to access Scutcs    private String URL      = "jdbc:mysql://127.0.0.1:3306/studentdb";    The user name of the MySQL configuration is    private String user     = "root";     The password for MySQL configuration is    private String password = "root";    private static Connection conn = null;     /**     * Open Data connection */public    void Opendbconnect ()    {        try         {          //load driver         Class.forName (driver);         Connect Database         if (conn = = NULL | | conn.isclosed ())         {         conn = drivermanager.getconnection (URL, user, password); c25/>}         if (!conn.isclosed ())          {         System.out.println ("succeeded connecting to the database!");         }        }        catch (Exception ex)        {        System.out.println ("Failed to Access database");}    }

2. Add Data

    /**     * Insert Data     * @param student     * @throws SQLException * *    /public void insertstudent (student student) Throws SQLException    {     Statement Statement = Conn.createstatement ();         The SQL statement to execute         

+ student.getage () + ", '" + student.getclassname () + "')"; Statement.execute (SQL); }

3. Delete Data

    /**     * Delete Data     * @param student     * @throws SQLException * * * public    void deletestudent (int id) throws SQLException    {     Statement Statement = Conn.createstatement ();         SQL statement to execute         String sql = "Delete from student where id =" + ID;         Statement.execute (SQL);    }

 4. update data

/** * Modify data * @param student * @throws SQLException * * Public voi         D updatestudent (Student Student) throws SQLException {Statement Statement = conn.createstatement ();         SQL statement to execute String sql = "update student set";         Student Name if (student.getstudentname () = null &&!student.getstudentname (). Trim (). Equals ("")) {         SQL + = "Studentname = '" + student.getstudentname () + "',";         }//Age if (student.getage ()! = 0) {SQL + = "ages =" + student.getage () + ",";          }//Grade if (student.getclassname () = null &&!student.getclassname (). Trim (). Equals ("")) {         SQL + = "classname = '" + student.getclassname () + "',";         } sql = sql.substring (0, Sql.length ()-1);         sql = SQL + "WHERE id =" + student.getid ();    Statement.execute (SQL); }    

5. Querying Data

    /**     * Modify Data     * @param student     * @throws SQLException * * * public    void Querystudent (String studentname) Throws SQLException    {     Statement Statement = Conn.createstatement ();         SQL statement to execute         String sql = "SELECT * from student where  studentname = '" + studentname + "'";         ResultSet rs = statement.executequery (sql);         while (Rs.next ()) {             //Select sname This column of data             String studentnamers = rs.getstring ("Studentname");             String agers         = rs.getstring ("Age");             String classnamers   = rs.getstring ("classname");             Output results             System.out.println ("Student Name:" + Studentnamers + ", Age:" + agers  + ", class:" + Classnamers);        }    }

The above describes the Java access to MySQL simple code, more in-depth after we explain. This article just takes you to a simple introduction

Conclusion

    • Benefit, learned how to access MySQL database with Java

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )

Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4561918.html

  

[Javaweb Basic] 003.JAVA access MySQL Database

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.