MySQL---Database get started with the Big God Series (ii)-use Java to add and revise MySQL

Source: Internet
Author: User

The previous section has learned to make simple additions and deletions to MySQL, then, how we implement Java to the database operation additions and deletions to change it.

This section will use the Java demo to add and revise MySQL.

In simple terms, it is divided into 4 steps:

1, load connector (drive) through the driver class (preferably with class reflection to load, more flexible)

2, establish a connection with the database

3. Get the Statement object

4, the operation of the database (increase and deletion check)


In fact, the first step can not be written now, the high version of MySQL has been in the internal help us to write the first step, but for better compatibility (compatible with the lower version of MySQL) we'd better write the first step.


Let's take a look at the original database table:




Use Java to add and revise databases:


Package Cn.hncu.sqlhello;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.SQLException; Import Java.sql.statement;import Org.junit.test;//import Com.mysql.jdbc.driver;public class DbAccess {@Testpublic void Hello () throws ClassNotFoundException, SQLException {//1, Load connector (drive) Driver class.forname ("Com.mysql.jdbc.Driver") ;//With class reflection loading, more flexible//2, establish connection//string URL = "Jdbc:mysql://127.0.0.1:3306/hncu";//This sentence is the default encoding String URL = "jdbc:mysql:// 127.0.0.1:3306/hncu?useunicode=true&characterencoding=utf-8 ";//Use the specified encoding to connect connection con = Drivermanager.getconnection (URL, "root", "1234");//These 3 parameters are: Connection string, user name, password//3, Get Statement object Statement st = Con.createstatement () ;//Add a row of data to the table Hncu////4 operation of the database (increased deletion) String sql = "INSERT into stud values (' 1010 ', ' Yang over ', 26, ' martial arts ')";//string sql = "Insert int O Stud values (' 1010 ', ' Yang over ', 26, ' martial arts '); The/SQL statement is the same as the semicolon. But NOTE!!! You cannot add more than one statement at a time! (not in the middle with a semicolon) st.execute (SQL);////delete instead: St.excute () method}}


If you want to run more than one statement, it's like this, running the 4th step several times, can achieve the same effect:
Example:
sql = "INSERT into stud values (' 1011 ', ' Yang over 1 ', 26, ' martial arts ');"; St.execute (sql); sql = "INSERT into stud values (' 1011 ', ' Yang over 1 ', 26, ' martial arts ');"; St.execute (SQL);


After running the stud table:




Deletion and modification is the same as adding, just swap the SQL string for a bit (SQL that string is a SQL action statement).


Query is different!

The Java query SQL database statement code is as follows:

Package Cn.hncu.sqlhello;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.ResultSet;import Java.sql.sqlexception;import Java.sql.statement;import Org.junit.test;//import Com.mysql.jdbc.Driver;public class dbAccess {@Testpublic void Hello () throws ClassNotFoundException, SQLException {//1, Load connector (drive) Driver Class.forName (" Com.mysql.jdbc.Driver ");//With class reflection loading, more flexible//2, establish connection//string URL =" Jdbc:mysql://127.0.0.1:3306/hncu ";//This sentence is using the default encoding String url = "Jdbc:mysql://127.0.0.1:3306/hncu?useunicode=true&characterencoding=utf-8";//connection Connection con = with specified encoding Drivermanager.getconnection (URL, "root", "1234");//These 3 parameters are: Connection string, user name, password//3, Get Statement object Statement st = Con.createstatement () ;//Add a row of data to the table Hncu//4 the database (query) String sql = "SELECT * from stud where sname like '% three ' or sname like '% four ';"; ResultSet rs = st.executequery (sql), while (Rs.next ()) {//Why not Hasnext, because RS is like a pointer, RS position does not point to the first data,//to the start address, So you can't use Hasnext to judge, and you have to skip the first one not to read. String sno = rs.getstring (1);//read by specifying the column number. The ordinal number of the first column is 1. String sname = rS.getstring ("sname");//Specifies the column name in the way to read int age = Rs.getint ("Age"); String saddress = rs.getstring (4); System.out.println (sno+ "," +sname+ "," +age+ "," +saddress);}}}


Here is the query sname with three or four of the data.



The other is the case, to change the SQL statement can be modified by the corresponding additions and deletions!



MySQL---Database get started with the Big God Series (ii)-use Java to add and revise MySQL

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.