Connecting to mysql using Java JDBC
JDBC (Java Data Base Connectivity, java database connection) is a Java API used to execute SQL statements. it can provide unified access to multiple relational databases, it consists of a group of classes and interfaces written in Java. JDBC provides a standard API for database developers, allowing database developers to write database applications using pure Java APIs and run them across platforms without being limited by database vendors. Advantages:
- Easy to operate:Developers do not need to use complex drives to call commands and functions;
- High portability:JDBC supports different relational databases
- Good versatility:The JDBC-ODBC bridge driver replaces the JDBC function with ODBC;
- Object-oriented:Common JDBC database connections can be encapsulated into a class and called directly when used.
Package DAO; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. SQLException; import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. list; import java. util. map; import com. mysql. jdbc. statement; public class JDBC {protected Connection conn; protected Statement; public static Connection getConnection () throws Exception {Connection connection = null; // create the Connection object try {Class for database Connection. forName ("com. mysql. jdbc. driver "); // load the Mysql data Driver/*** 127.0.0.1 indicates the local machine. if it is linked to a remote service machine, enter the default mysql port number of the remote machine ip 3306, test database name * user database user name password */connection = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/test", "user", "password"); // create a data connection} catch (Exception e) {e. printStackTrace (); throw new Exception ("failed to link mysql data");} return connection; // return the established database connection}/*** insert data records into mysql return the number of inserted data ** @ param SQL * SQL statement to be inserted * @ return count insert data number of * @ throws Exception */public int insert (String SQL) throws Exception {conn = getConnection (); // connect to the database try {statement = (Statement) conn. createStatement (); // create the Statement object int count = statement.exe cuteUpdate (SQL) for executing static SQL statements; // the SQL Statement conn for executing the insert operation. close (); // close the database connection return count; // return the number of inserted data} catch (SQLException e) {e. printStackTrace (); throw new Exception ("failed to insert data ");}} /*** update the number of records that meet the requirements ** @ param SQL * SQL statement for updating data * @ return count number of updated data * @ throws Exception */public int update (String SQL) throws Exception {conn = getConnection (); // connect to the database try {// create a Statement object for executing static SQL statements, statement = (Statement) conn. createStatement (); int count = statement.exe cuteUpdate (SQL); // SQL statement for executing the update operation, conn. close (); // close the database connection return count; // return the number of updated data} catch (SQLException e) {e. printStackTrace (); throw new Exception ("failed to update data") ;}}/*** Query Database, return the required record data ** @ param SQL statement for data query * @ throws Exception * @ return list */public ListQuery (String SQL) throws Exception {conn = getConnection (); // connect to the database try {statement = (Statement) conn. createStatement (); // create the Statement object ResultSet rs = statement.exe cuteQuery (SQL) for executing the static SQL Statement; // execute the SQL query Statement to return the List of result sets of the query dataList = ResultSetToList (rs); conn. close (); // close the database connection return list;} catch (SQLException e) {e. printStackTrace (); throw new Exception ("failed to query data") ;}/ * delete records that meet the requirements, output status * // ***** @ param SQL statement for deleting data * @ return count returns the number of deleted data * @ throws Exception */public int delete (String SQL) throws Exception {conn = getConnection (); // connect to the database try {statement = (Statement) conn. createStatement (); // create the Statement object int count = statement.exe cuteUpdate (SQL) for executing static SQL statements; // run the SQL delete Statement conn. close (); // close the database connection return count; // return the number of deleted data} catch (SQLException e) {e. printStackTrace (); throw new Exception ("failed to delete data ");}} /*** query by page * @ param SQL the SQL statement to be searched * @ param page number * @ param count number of data entries * @ return List* @ Throws Exception */public ListFindByPage (String SQL, int page, int count) throws Exception {conn = getConnection (); // connect to the database PreparedStatement pre = conn. prepareStatement (SQL); pre. setMaxRows (count); ResultSet rs = pre.exe cuteQuery (); if (page <1) {rs. absolute (0);} else {page = page * count-1; rs. absolute (page);} ListList = ResultSetToList (rs); return list;}/*** ResultSet to List */public ListResultSetToList (ResultSet rs) throws SQLException {if (rs = null) return Collections. emptyList (); ResultSetMetaData md = rs. getMetaData (); // get the structure information of the result set (rs) int columnCount = md. getColumnCount (); // returns the List of columns in the ResultSet object.List = new ArrayList(); Map
RowData = new HashMap
(); While (rs. next () {rowData = new HashMap
(ColumnCount); for (int I = 1; I <= columnCount; I ++) {rowData. put (md. getColumnName (I), rs. getObject (I);} list. add (rowData) ;}return list ;}}