Java encounters MySQL
1. Simple Java Bean ==> can be serialized. The materials to be prepared are:
① Define n private member variables. in connection to the database, we need to define n fields corresponding to the table we created.
② Implement the set and get methods for all member variables.
③ Implement the construction method without parameters and with full parameters.
④ Rewrite the toString method.
⑤ Implement the interface Serializable.
2. In SQL, a record is equivalent to an object, so you need to save it with a new object.
3. steps required to connect to the database in Java <because MySQL is not a built-in database, we need to manually export the package>
① Import database package ==> create folder ==> paste the jar package of MySQL database to the folder ==> right-click Build Path ==> configure Build Path ==> Libraries ==> AddJARs.
② Create a class file.
③ After preparation, we have been connecting to the database through Java for a long time!
A: load the driver.
Eg: Class. forName ("org. gjt. mm. mysql. driver "); // org. gjt. mm ==> special MySQL writing method; mysql ==> name of the folder we just created; Driver ==> Class Name
B: connect to the database
Eg: Connection con = null;
String url = "jdbc: mysql: // localhost: 3306/Lee_My"; // jdbc: mysql ==> what type of database; localhost ==> IP address of the connected data; 3306 ==> port number; Lee_My ==> Database Name
String user = "root"; // user Name
String pwd = "123"; // Password
Con = DriverManager. getConnection (url, user, pwd); // open the database
For specific instances, see the following code:
Package com. fs. test; // import java. SQL. driverManager; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; import java. util. arrayList; import java. util. list; import javax. swing. JOptionPane; import com. fs. po. stu; public class Test {private Connection getCon () {// write a method to connect to the database. Two steps are required: Connection con = null; // (1) load the driver try {Class. ForName ("org. gjt. mm. mysql. driver "); // (2) connect to the Database" jdbc: mysql: // 192.168.1.17: 3306/Lee_My "; String url =" jdbc: mysql: // localhost: 3306/fs_service "; String user =" root "; String pwd =" 123 "; // DriverManager: Manages basic services of a set of JDBC drivers. Con = DriverManager. getConnection (url, user, pwd); // getConnection (String url, String user, String password) // tries to establish a connection to the given Database URL. DriverManager tries to select an appropriate driver from the registered JDBC driver set .} Catch (Exception e) {e. printStackTrace ();} return con;} // insertprivate void add () throws Exception {/** Connection (session) to a specific database ). Execute the SQL statement in the connection context and return the result. */Connection con = this. getCon (); // connect to the database Statement st = con. createStatement (); // The object used to execute a static SQL Statement and return the result it generates. // The createStatement () method in interface Connection is used to create a Statement object to send SQL statements to the database. Int no = 0; no = no + st.exe cuteUpdate ("insert into stu (name, age, xueHao) values ('xiaoming1', 23, 7 )"); no = no + st.exe cuteUpdate ("insert into stu (name, age, xueHao) values ('xiaoming2', 24, 8 )"); // action of the executeUpdate (String SQL) method in the Statement interface: execute a given SQL Statement. // This Statement may be an INSERT, UPDATE, or DELETE Statement, or an SQL statement (such as an SQL DDL statement) that does not return any content ). Con. close (); // close the stream JOptionPane. showMessageDialog (null, no); // inform the user that something has occurred. A dialog box is displayed, with the message no displayed.} // deleteprivate void delete () throws Exception {// same as the add () method Connection con = this. getCon (); Statement st = con. createStatement (); int no = 0; no = no + st.exe cuteUpdate ("delete from stu where id> 1"); con. close (); JOptionPane. showMessageDialog (null, no);} private void update () throws Exception {// Connect with the add () method Ion con = this. getCon (); Statement st = con. createStatement (); int no = 0; no = no + st.exe cuteUpdate ("update stu set name = 'xiaoming', age = 30 where id> 1"); con. close (); JOptionPane. showMessageDialog (null, no);} private List <Stu> select () throws SQLException {// create a collection List to save the query records Connection con = this. getCon (); // connect to the database Statement st = con. createStatement (); // The object used to execute a static SQL Statement and return the result it generates. // Execute the given SQL Statement using the executeQuery (SQL) method in the Statement interface. This Statement returns a single ResultSet object. // Return: The ResultSet object that contains the data generated by the given query; it cannot be null ResultSet r = st.exe cuteQuery ("select id, name, age, xueHao from stu "); list <Stu> list = new ArrayList <Stu> (); // creates an ArrayList object. // uses the while loop to traverse each record while (r. next () {// r. next () points to the next record. The first point is the field, and the int id = r. getInt (1); // output id column, that is, String name = r. getString (2); // output name column, that is, the second column in the table, int age = r. getInt (3); // output age column, that is, int xueHao = r. getInt (4); // output the xueHao column, that is, the fourth column list in the table. add (new Stu (id, name, age, xueHao); // a record is an object, so the new objects of each record are loaded into the ArrayList collection} con. close (); // close the stream return list; // return the list set} public static void main (String [] args) throws Exception {List <Stu> list = new Test (). select (); for (Stu stu: list) {// The set traverses the System. out. println (stu. toString ());}}}