Simply put the steps to operate:
1. Connect to the database
2. Send SQL statements to the database
3. Execute SQL statements
Here is an example:
There is a student number (Id), name (Name), gender (Sex), address (Address), phone (Phone), and major (Dept).
Here, write this table as a student information class (Info_student)
(Please make sure to read the examples first, otherwise you may not understand some parts of the code)
To implement manipulation, we must first connect to the database, because each operation requires a connection operation, so we directly encapsulate the connected operation in a class and call it directly when we need to connect.
Database connection class:
import java.sql.Connection; import java.sql.DriverManager; public class DB_Helper { public static Connection connect = null; static { try { Class.forName("com.mysql.jdbc.Driver"); // Load the MYSQL JDBC driver// Observe the difference between the following two statements, // connect = // DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", ""); connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8", "root", ""); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } } public static Connection getConnection() { return connect; } } The database has been connected, so the next step is to send SQL statements and execution statements.
The sending statement uses the PreparedStatement object and Connection object operations to prepareStatement()
Execute statements to use the PreparedStatement object's operation execute()
Tip: The following are some descriptions of objects. You can read the code first and then come back when you encounter them.
******************************
PreparedStatement
An object that represents a precompiled SQL statement.
SQL statements are precompiled and stored in PreparedStatement objects. This statement can then be executed efficiently multiple times using this object.
******************************
Connection
Connection (session) to a specific database. Execute SQL statements in the connection context and return the result.
The database of the Connection object can provide information describing its tables, supported SQL syntax, stored procedures, this connection function, and so on.
***************************
The following code is to implement the operation of adding, deleting, modifying and checking student information in the database.
1. Add
public void add(Info_student student) throws SQLException{ // Connection (session) to a specific database. Connection conn = (Connection) DB_Helper.getConnection(); String sql = "insert into student(Sno,Sname,Ssex,Saddress,Sphone,Sdept) values(?,?,?,?,?,?)"; // Create a PreparedStatement object to send parameterized SQL statements to the database. PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); /* * void setBigDecimal(int parameterIndex,BigDecimal x)throws SQLException * Set the specified parameter to the given Java String value. When sending this value to the database, the driver converts it to a SQL VARCHAR* or LONGVARCHAR value (depending on the size of the parameter relative to the driver's limit on the VARCHAR value). */ ptmt.setString(1, student.getId()); ptmt.setString(2, student.getName()); ptmt.setString(3, student.getSex()); ptmt.setString(4, student.getAddress()); ptmt.setString(5, student.getPhone()); ptmt.setString(6, student.getDept()); // Execute the SQL statement ptmt.execute() in this PreparedStatement object; }2. Delete
public void delete(String id) throws SQLException{ Connection conn = (Connection) DB_Helper.getConnection(); String sql = "delete from student where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, id); ptmt.execute(); }3. Change
public void update(Info_student student) throws SQLException{ Connection conn = (Connection) DB_Helper.getConnection(); String sql = "update student set Sname=?,Ssex=?,Saddress=?,Sphone=?,Sdept=? where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, student.getName()); ptmt.setString(2, student.getSex()); ptmt.setString(3, student.getAddress()); ptmt.setString(4, student.getPhone()); ptmt.setString(5, student.getDept()); ptmt.setString(6, student.getId()); ptmt.execute(); }4. Check
public Info_student search(String id) throws SQLException{ Info_student student = null; Connection conn = (Connection) DB_Helper.getConnection(); String sql = "select * from student where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, id); /* * ResultSet executeQuery()throws SQLException * Here PreparedStatement Executes an SQL query in the object and returns the ResultSet object generated by the query. */ /* * public interface ResultSet extends Wrapper * A data table representing the database result set, which is usually generated by executing statements that query the database. The ResultSet object has a cursor pointing to its current data row. * Initially, the cursor is placed before the first line. The next method moves the cursor to the next row; because the method returns false when the ResultSet object has no next row, it can be used in a while loop to iterate over the result set. * */ ResultSet rs = ptmt.executeQuery(); /* * boolean next()throws SQLException * Move the cursor forward by one line from the current position. * The ResultSet cursor is initially before the first line; * The next method is called the first line to become the current line; * The second call makes the second line to the current line, and so on. */ while(rs.next()){ student = new Info_student(); student.setId(rs.getString("Sno")); student.setName(rs.getString("Sname")); student.setSex(rs.getString("Ssex")); student.setAddress(rs.getString("Saddress")); student.setPhone(rs.getString("Sphone")); student.setDept(rs.getString("Sdept")); } return student; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.