mysql query statement and JDBC
One, the query keyword
1. Grouping: GROUP BY, which is grouped and displayed according to a certain column. There are several groups of data in the group. It can be used with aggregate functions.
2. Conditions: HAVING is similar to WHERE, but HAVING can only be used after GROUP BY, and judgment characters can be used
3. LIMIT: After the entire SELECT statement, it controls the number of displayed items. Two parameters, the first one starts from the first one, and the second is how many items to display.
4. Execution order of query statement keywords:
SELCET -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY -> LIMIT
5. Code
SELECT emp.job,emp,hiredate,avg(emp.sal+IFNULL(comm,0)) AS avg_sal;
FROM lan_ou.emp
WHERE hiredate BETWEEN '1980-01-01' AND '1981--12-31'
GROUP BY emp.job
HAVING avg_sal > 1000
ORDER BY avg_sal ASC;
Two, JDBC: JAVA connection database
1. Load the driver: Class.forName("com.mysql.cj.jdbc.Driver");//Load the driver class into the memory through the class name
2. Get the connection: Connection conn = DriverManager //Parameters: URL, account name, password
.getConnection("jdbc:mysql://localhost:3306/lan_ou","root","123456");
3. Get the object that executes the SQL statement: Statement state = conn.createStatement();
4. Execute the sql statement:
String sql = "SELECT * FROM stu;"; //The sql statement to be executed is assigned to the string variable
int rows = statement.executeUpdate(sql);//The return value is int type, indicating the number of rows affected by the sql statement
ResultSet resultSet = state.executeQuery(sql); //The result set returned by the resultSet query statement
while(resultSet.next()){
String id = resultSet.getString('sid');//Get data according to the original type, you can perform operations such as calculations
System.out.println(resultSet.getObject("sname");//If you use Object, you cannot calculate and other operations
};
5. Close resources: state.close(); , conn.close();
Three, wheels: JDBCUtil
package com.lanou3g.code0426 ;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.Statement ;
public class JDBCUtil {
private static Connection conn ; //Connection object static { // Static code block // When the program loads this class for the first time, the static code block of this class will be executed first // and the entire project will only be executed once as long as it does not stop // Load the mysql driver and execute it once try { Class.forName ( " com.mysql.cj.jdbc.Driver ") ; conn = DriverManager.getConnection ( "jdbc:mysql://localhost:3306/lan_ou" , "root" ,
"123456");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Statement getStatement(){
try {
Statement statement = conn.createStatement();
return statement;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void closeConn(){
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeResource(Statement statement){
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}