This article describes the Java method of operating Mysql. Share it for your reference. The specific analysis is as follows:
Unlike the same thing as C# manipulating databases, if Java wants to divide the statements that manipulate databases into two types:
One is a result-based select statement, and the other is a result-based insert into, update, delete and other statements.
1. If it is a result select statement, you need to define a ResultSet variable to be included, and use con.prepareStatement(sql).executeQuery(); to query, where con is a database connection variable, and java must be introduced in the program header. sql.*, the query statement must be processed by throwing an exception. The same is below, and I will not repeat it again. Then use the ResultSet variable to read the query result.
For example:
public String execute() { String sql = "select * from a where username='" + username + "'"; try { rs = con.prepareStatement(sql).executeQuery(); if (!rs.next()) { //… con.close(); } } catch (Exception e) { message = "Cannot connect to database!"; } }2. If it is a statement without result insert into, update, delete, etc., you do not need to define any variables. Please change the query method to:
con.createStatement().execute(sql);
Just
I hope this article will be helpful to everyone's Java programming.