Link Java to mySQL database for addition, deletion, modification, and query Java link mySQL database code
Modification and query are the same for addition.
Public class JDBCTest {/*** ResultSet encapsulates the JDBC result set and query results */@ Testpublic void testResultSet () {Connection connection = null; Statement statement = null; resultSet rs = null; try {connection = JDBCTools. getConnection (); statement = (Statement) connection. createStatement (); String SQL = "SELECT * FROM jdbc_1"; rs = statement.exe cuteQuery (SQL); while (rs. next () {System. out. print (rs. getInt (1) + "/t"); System. ou T. print (rs. getString (2) + "/t"); System. out. print (rs. getString (3); System. out. println () ;}} catch (Exception e) {e. printStackTrace ();} finally {JDBCTools. release (rs, statement, connection) ;}/ *** common update method * @ throws SQLException */public void updata (String SQL) throws SQLException {Connection connection = null; statement statement = null; try {connection = getConnection (); // get the Statement object statement = (S Tatement) connection. createStatement (); // execute SQL statement statement.exe cuteUpdate (SQL);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {try {// Close the statement object if (statement! = Null) {statement. close () ;}} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {// Close the database connection if (connection! = Null) {connection. close () ;}}}/*** 1. insert data to the specified table through jdbc ** @ throws Exception **/@ Testpublic void testStatement () throws Exception {// Obtain the database Connection connection = null; Statement statement = null; try {connection = getConnection (); // SQL statement to be executed String SQL = "INSERT INTO jdbc_1 (jname, addr) VALUE ('Lily', 'hunjiang ')"; // Obtain the Statement object statement = (Statement) connection. createStatement (); // execute the SQL statement. ExecuteUpdate (SQL);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {try {// Close the statement object if (statement! = Null) {statement. close () ;}} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {// Close the database connection if (connection! = Null) {connection. close () ;}}}/*** the DriverManager class is the driver management class that uses DriverManager to connect to the database and register the database driver ** @ throws Exception */@ Testpublic void testDriverManager () throws Exception {String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // read the properties file InputStream in = getClass (). getClassLoader (). getResourceAsStream ("jdbc. properties "); Properties properties = new Properties (); properties. load (in); driverClass = properties. getProperty ("driver"); jdbcUrl = properties. getProperty ("url"); user = properties. getProperty ("user"); password = properties. getProperty ("password"); Class. forName (driverClass); Connection connection Connection = (Connection) DriverManager. getConnection (jdbcUrl, user, password); System. out. println (connection);}/*** driver test. test the link driver ** @ throws SQLException **/@ Testpublic void testDriver () throws SQLException {// create Drivers implementation class Driver = new com. mysql. jdbc. driver (); String url = "jdbc: mysql: // localhost: 3306/jdbc"; Properties info = new Properties (); info. put ("user", "root"); info. put ("password", "root"); // call the ConnectionConnection connection = (Connection) driver of the driver interface. connect (url, info); System. out. println (connection);}/*** write a common method to obtain any Database link, you do not need to modify the source program ** @ throws ClassNotFoundException * @ throws injection * @ throws InstantiationException * @ throws SQLException * @ throws IOException */public Connection getConnection () throws InstantiationException, expiration, expiration, SQLException, IOException {String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // read the properties file InputStream in = getClass (). getClassLoader (). getResourceAsStream ("jdbc. properties "); Properties properties = new Properties (); properties. load (in); driverClass = properties. getProperty ("driver"); jdbcUrl = properties. getProperty ("url"); user = properties. getProperty ("user"); password = properties. getProperty ("password"); Driver driver = (Driver) Class. forName (driverClass ). newInstance (); Properties info = new Properties (); info. put ("user", user); info. put ("password", password); Connection connection Connection = (Connection) driver. connect (jdbcUrl, info); return connection ;}@ Testpublic void testConnection () throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, IOException {System. out. println (getConnection ());}}
Public class JDBCTools {/*** close query result * @ param rs * @ param statement * @ param conn */public static void release (ResultSet rs, Statement statement, Connection conn) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (statement! = Null) {try {statement. close () ;}catch (Exception e2) {e2.printStackTrace () ;}} if (conn! = Null) {try {conn. close () ;}catch (Exception e2) {e2.printStackTrace () ;}}/ *** database update method * @ param SQL */public void uodate (String SQL) {Connection connection = null; Statement statement = null; try {connection = JDBCTools. getConnection (); statement = (Statement) connection.createstatement({statement.exe cuteUpdate (SQL);} catch (Exception e) {e. printStackTrace ();} finally {JDBCTools. release (statement, Connection) ;}}/*** method for disabling database Connection * @ param statement * @ param conn */public static void release (Statement statement, connection conn) {if (statement! = Null) {try {statement. close () ;}catch (Exception e2) {e2.printStackTrace () ;}} if (conn! = Null) {try {conn. close () ;}catch (Exception e2) {e2.printStackTrace () ;}}/ *** write a common method to obtain any Database link, you do not need to modify the source program ** @ return * @ throws ClassNotFoundException * @ throws IllegalAccessException * @ throws InstantiationException * @ throws SQLException * @ throws IOException */public static Connection getConnection, classNotFoundException, SQLException, IOException {String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // read the properties file InputStream in = JDBCTools. class. getClassLoader (). getResourceAsStream ("jdbc. properties "); Properties properties = new Properties (); properties. load (in); driverClass = properties. getProperty ("driver"); jdbcUrl = properties. getProperty ("url"); user = properties. getProperty ("user"); password = properties. getProperty ("password"); Driver driver = (Driver) Class. forName (driverClass ). newInstance (); Properties info = new Properties (); info. put ("user", user); info. put ("password", password); Connection connection Connection = (Connection) driver. connect (jdbcUrl, info); return connection ;}}