This article describes the Java operation of MySQL method. Share to everyone for your reference. The specific analysis is as follows:
Unlike C # manipulation of the database, if it is to Java to manipulate the database statements are divided into two types:
One is a SELECT statement with a result, and there is a statement that has no result, such as insert Into,update,delete
1. In the case of a SELECT statement with a result, you need to define a resultset variable to be followed by using con.preparestatement (SQL). ExecuteQuery (); To query, where con is a database connection variable, In the program header must introduce java.sql.*, the query statement must be thrown through the exception to deal with, the same, no longer repeat, and then use the ResultSet variable to read the query results.
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
= ' Unable to connect to the database! ";
}
}
2. If there is no result of insert into,update,delete and other statements, you do not need to define any variables, note that the query method changes to:
Con.createstatement (). Execute (SQL);
Can
I hope this article will help you with your Java programming.