Use the JDBC Send INSERT statement to complete a single table "Add" action
To complete a single-table update operation by using JDBC to send an UPDATE statement
Use the JDBC Send DELETE statement to complete the single-table delete operation
Use JDBC to send a SELECT statement to complete a single-table "query" operation
Jdbcutils_v3.java
Import Java.io.ioexception;import java.io.inputstream;import java.sql.connection;import Java.sql.DriverManager; Import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import java.util.properties;/** * Provides methods for getting connections and freeing resources */public class Jdbcutils_v3 {private static string driver;private static string Url;private the static string username;private static string password;/* * Block Load profile information */static {try {//1. Get the ClassLoader class from the current class Loader ClassLoader = JDBCUtils_V3.class.getClassLoader ();//2. Get an input stream through the ClassLoader method InputStream is = Classloader.getresourceasstream ("db.properties");//3. Create a Properties Object properties props = new properties ();//4. Load input stream props.load (is);//5. Gets the value of the relevant parameter driver = Props.getproperty ("driver"); URL = props.getproperty ("url"); username = Props.getproperty ("username");p assword = props.getproperty ("password");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Get Connection method * * @return */public static Connection getconnection () {Connection conn = null;try {class.forname (driver); conn = drivermanager.getconnection (URL, username, password);} catch (Exception e) {E.printstacktrace ();} Return conn;} public static void Release (Connection conn, PreparedStatement pstmt, ResultSet rs) {if (rs! = null) {try {rs.close ();} cat CH (SQLException e) {e.printstacktrace ();}} if (pstmt! = null) {try {pstmt.close ();} catch (SQLException e) {e.printstacktrace ()}} IF (conn! = null) {try {conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}}
Testutils.java
Import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Org.junit.test;import Cn.itheima.jdbc.jdbcutils_v1;import Cn.itheima.jdbc.JDBCUtils_ V2;import cn.itheima.jdbc.jdbcutils_v3;/** * Test Tool class */public class Testutils {/* * update information by ID */@Testpublic void testupdate Yid () {Connection conn = null; PreparedStatement pstmt = null;try {///1. Get Connection conn = Jdbcutils_v3.getconnection ();//2. Write SQL statement String sql = "Update Tbl_user Set upassword=? where uid=? "; /3. Gets the Execute SQL statement Object pstmt = conn.preparestatement (sql);//4. Set Parameters pstmt.setstring (1, "999");p Stmt.setint (2, 1);//5. Perform an update operation int row = Pstmt.executeupdate (); if (Row > 0) {System.out.println ("Update succeeded! ");} else {System.out.println ("Update failed! ");} Freeing resources to put them here, okay? "No drop!" "} catch (Exception e) {throw new RuntimeException (e);} finally {//6. Free Resource jdbcutils_v2.release (conn, pstmt, NULL);}} /* * Delete Information by ID method */@Testpublic void Testdeletebyid () {Connection conn = null; PreparedStatement pstmt = null;try {//1. Get Connection conn = jdbcutils_v3.getconnection ();//2. Write SQL statement String sql = "Delete from Tbl_user where uid=?"; /3. Gets the Execute SQL statement Object pstmt = conn.preparestatement (sql);//4. Set parameter Pstmt.setint (1, 3);//5. Perform the delete operation int row = Pstmt.executeupdate ( if (Row > 0) {System.out.println ("Delete succeeded! ");} else {System.out.println ("Delete failed! ");} Freeing resources to put them here, okay? "No drop!" "} catch (Exception e) {throw new RuntimeException (e);} finally {//6. Free Resource jdbcutils_v2.release (conn, pstmt, NULL);}} /* * Add user Information method */@Testpublic void Testadd () {Connection conn = null; PreparedStatement pstmt = null;try {///1. Get Connection conn = Jdbcutils_v2.getconnection ();//2. Write SQL statement String sql = "INSERT INTO TBL _user values (null,?,?) "; /3. Get Execute SQL Statement Object pstmt = conn.preparestatement (sql);//4. Set Parameters pstmt.setstring (1, "Lisi");p stmt.setstring (2, "Lisi");// 5. Perform the insert operation int row = Pstmt.executeupdate (), if (Row > 0) {System.out.println ("Add success! ");} else {System.out.println ("Add failed! ");} Freeing resources to put them here, okay? "No drop!" "} catch (Exception e) {throw new RuntimeException (e);} finally {//6. Releasing the Resource Jdbcutils_v2.release (CONN, pstmt, null);}} /** * Query user information by ID */@Testpublic void Testfinduserbyid () {Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null;try {//1. Get Connection conn = Jdbcutils_v1.getconnection ();//2. Write SQL statement String sql = "SELECT * FROM Tbl_user whe Re uid=? "; /3. Get Execute SQL Statement Object pstmt = conn.preparestatement (sql);//4. Set parameter Pstmt.setint (1, 1);//5. Perform a query operation RS = Pstmt.executequery ();// 6. Processing result set while (Rs.next ()) {System.out.println (rs.getstring (2) + "----" + rs.getstring ("Upassword"));} Freeing resources to put them here, okay? "No drop!" "} catch (SQLException e) {e.printstacktrace ();} finally {//7. Release Resources Jdbcutils_v1.release (conn, pstmt, RS);}}}
Web09_mysql Multi-table &jdbc