Any project can not be separated from the data, but for the storage of data and other operations, it will use the database.
Here is the main operation for the MySQL database.
1. Software
Of course, first of all to download MySQL, in order to operate more convenient, here is recommended a more convenient auxiliary software navicat for MySQL,
Look at the name to know specifically for MySQL design. (image size is too large, and can not find the option to reduce size, simply do not map)
1.1
First, if MySQL does not create a new connection when it is created, then enter the software and create a new connection.
This will require input hostname, the local server is 127.0.0.1;
Then the port, which is 3306 by default, can be changed on its own, but outside of 0~1023, because these are reserved for system software;
Then is the user name and password, convenient memory, generally write root,root.
After that, enter this connection, create a new database such as ECO, after the operation for convenience, in Navicat for MySQL operation.
1.2
After entering Navicat for MySQL, establish a connection to the previous connection, fill in the previous hostname, port, username,
Password and other necessary information.
Open the database eco, the build table of the table, the import import, because all the Chinese I will not repeat.
Once the database is built, it is the operation of the database in Java.
2.java Project establishes a connection to the database
First we want to download a MySQL-driven jar package, drag it under the project, then right-click Bulid Path;
1 Public classMySQL {2 Private Static FinalString URL = "Jdbc:mysql://127.0.0.1:8090/eco?useunicode=true&characterencoding=utf-8&usessl=false";3 Private Static FinalString USER = "root";4 Private Static FinalString PASSWORD = "root";5 Private StaticConnection Conn;6 StaticConnection GetConnect () {7 Try {8 //load MySQL driver9Class.forName ("Com.mysql.jdbc.Driver");Ten //get a connection to the database eco Oneconn =drivermanager.getconnection (URL, USER, PASSWORD); A}Catch(Exception e) { - e.printstacktrace (); - } the returnConn; -}
17}
A static method GetConnect () is defined here to call the connection to the specified database at any time in other classes, and we can
To see that there are three parameters in the method of obtaining the database eco connection, the following explains the meaning of this URL parameter:
JDBC: Refers to the use of JDBC method to establish a connection to the database;
Mysql:hostname, because it is local, so it is still 127.0.0.1;
Eco: Database name;
Unicode~: The method of resolution, here is utf-8;
UseSSL: This is the version compatibility process for JDBC and MySQL.
3. Read table data into a single collection
1 Public classDo {2 Public StaticList userlist () {3list<user> list =NewArraylist<user>();4 //calling a method to implement a connection to a database5Connection conn =Mysql.getconnect ();6 Try {7Statement st =conn.createstatement ();8 //Create a new query statement to filter out the user, password fields from the Table1 table9ResultSet rs = st.executequery ("Select User,password from table1");Ten while(Rs.next ()) { One //adds a filtered record to the collection, AUser User =NewUser (); -User.setuser (rs.getstring ("User")); -User.setpassword (rs.getstring ("Password")); the list.add (user); - } - rs.close (); - st.close (); +}Catch(Exception e) { - e.printstacktrace (); + } A //This method eventually returns a collection of all records at returnlist; - } -}
Here, we first build a list collection that adds the table's data to the collection as a property of the user object.
Finally, this method returns a collection containing all user information (users, password).
In this way, we have saved the information in the database in a list collection, and then we will operate on this set, and then implement the
To the database of the increase, deletion, change, check.
MySQL Java database