Basic methods of Java Operational database

Source: Internet
Author: User

The development tool for Eclipse, only the database driver is Mysql-connector-java-5.1.8-bin.jar

The first step is to refer to the MySQL driver in the project directory of Eclipse

Drive: https://dev.mysql.com/downloads/connector/j/

Select Build path and select Configure Build Path

Then select Libraries, select Add External Library, find the database driver, confirm, OK

Then the next test code drives the database.

 Public class Test {    publicstaticvoid  main (String []args) {        try {            //MySQL Database setup driver type            class.forname ("Com.mysql.jdbc.Driver");             System.out.println ("MySQL database driver loaded successfully");                                }         Catch (ClassNotFoundException e) {            e.printstacktrace ();         }}}

The driver code runs once, and then the database is operational.

Basic operation modification and deletion, the principle is the same

Create a Connection object first

NULL ; String User= "root"; // User name String password= "19980713"; // Password String url = "jdbc:mysql://localhost:3306/task?user=" +user+ "&password=" +password+ "&useUnicode=true& Characterencoding=utf-8 "; // get a connected statement Try {     = (Connection) drivermanager.getconnection (URL);   } Catch (SQLException e) {      e.printstacktrace ();   }

Once the connection is made, the connection is manipulated.

Query records

String sql = "Select id,name,job,department,state,authority from Worker;";Try{PreparedStatement PS=(PreparedStatement) connection.preparestatement (SQL);//Pre-compilation process ResultSet RS= Ps.executequery ();//Query general with ExecuteQuery, return isresult set of the query//here's how to work with the result set                      while(Rs.next ()) {count++; Vector v=NewVector (); V.add ((Rs.getstring (1))); V.add ((Rs.getstring (2))); V.add ((Rs.getstring (3))); V.add ((Rs.getstring (4))); V.add ((Rs.getstring (5))); V.add ((Rs.getstring (6)));                     Worker.add (v); }                }Catch(SQLException E1) {e1.printstacktrace (); }

Inserting records

The rest of the operation is not affixed to the code, and so on, I first prepare a class six to go, will not wait to update

Basic methods of Java Operational database

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.