JDBC simple additions and deletions for testing under Eclipse console

Source: Internet
Author: User

1. Now create a table in MySQL

2. First create a class

The imported Package

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;


publicclass dbtil {}

3. Then instantiate each object

1   //username and password are the user names and passwords that connect to the database2    Private StaticString username = "123";3    Private StaticString password = "12345";4    //connect to MySQL address5    Private StaticString url = "Jdbc:mysql://localhost:3306/mydb";6    //connection to MySQL driver7    Private StaticString dirver = "Com.mysql.jdbc.Driver";8    //to create an object when a connection is established9     StaticConnection conn =NULL;Ten    //Create an PreparedStatement instance to execute the SQL statement One    StaticPreparedStatement PreparedStatement =NULL; A    //result Set Object -    StaticResultSet rs =NULL;

4. Then write the query table method

1 //See table2     Public Static voidcomtrue () {3String sql = "SELECT * from User";4         Try {5 Class.forName (dirver);6conn =drivermanager.getconnection (Url,username,password);7PreparedStatement =conn.preparestatement (SQL);8rs =preparedstatement.executequery ();9              while(Rs.next ()) {TenSystem.out.println (Rs.getstring (4) + "" "+rs.getstring (3) +" "+rs.getstring (2) +" "+rs.getstring (1)); One             } A System.out.println (); - Close (RS, Conn, preparedstatement); -              the}Catch(Exception e) { - E.printstacktrace ();  -         }        -}

5. Look at the results of the console output:

6. Then write the added method:

1   //Add2      Public Static voidAdd () {3String sql = "INSERT into user (Id,username,age,sex) value (?,?,?,?)";4         Try {5             intresult = 0;6 Class.forName (dirver);7conn =drivermanager.getconnection (Url,username,password);8PreparedStatement =conn.preparestatement (SQL);9Preparedstatement.setint (1, 5);TenPreparedstatement.setstring (2, "Xiaoming"); OnePreparedstatement.setint (3, 10); APreparedstatement.setstring (4, "male"); -result =preparedstatement.executeupdate (); -System.out.println ("Insert succeeded"); the Close (RS, Conn, preparedstatement); -}Catch(Exception e) { - e.printstacktrace (); -         } +}

7. Add a row of data with ID 5 on top of the original table to see the result:

Look at the added success!

8. After removing the next implementation:

1  //Delete2      Public Static voidDelete () {3String sql = "Delete from user where id = 4";4         Try {5 Class.forName (dirver);6conn =drivermanager.getconnection (Url,username,password);7PreparedStatement =conn.preparestatement (SQL);8 preparedstatement.executeupdate ();9System.out.println ("Delete succeeded! ");Ten}Catch(Exception e) { One e.printstacktrace (); A         } -}

Look at the results:

Delete Success! Male 20 31 female 30 Li 42 male Mike 3 male 10 Xiao Ming 5

9. Modify the Implementation method:

1   //Modify2      Public Static voidAlter () {3String sql = "Update user set username= ' Little red ' where id = 4";4         5         Try {6 Class.forName (dirver);7conn =drivermanager.getconnection (Url,username,password);8PreparedStatement =conn.preparestatement (SQL);9 preparedstatement.executeupdate ();Ten}Catch(Exception e) { One             //TODO auto-generated Catch block A e.printstacktrace (); -         } -}

Look at the results:

Male 20 31 female 30 Li 42 man Mike 3 male 10 Little Red 5

Change it to Little red!

10. If you want to view a row of data:

Query to query the first row of data as an example

1   //Enquiry2      Public Static voidInquire () {3String sql = "SELECT * from user limit 0, 1";4         Try {5 Class.forName (dirver);6conn =drivermanager.getconnection (Url,username,password);7PreparedStatement =conn.preparestatement (SQL);8rs =preparedstatement.executequery ();9              while(Rs.next ()) {Ten System.out.println ( OneRs.getstring (1) A+rs.getstring (2) -+rs.getstring (3) -+rs.getstring (4)); the             } -}Catch(Exception e) { -             //Todo:handle Exception -         } +}

Execution Result:

1 Sheets of 320 men

11. Test Method:

1  //Test2      Public Static voidMain (string[] args) {3         /*Delete ();*/4         /*Add ();5 Alter ();*/6         /*inquire ();*/7         /*comtrue ();*/8     }9     Ten}

12. Summary:

Through the console to run the test to achieve a simple JDBC connection, add and delete to change the operation, as a beginner, I think this test is necessary, the subsequent JDBC connection operation will be based on this

(The author small white, do not like to spray, small white come in, the Big God walk away)

  

JDBC simple additions and deletions for testing under Eclipse console

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.