l JDBC
l JDBC
*
* MyJDBCUtils.getConnection()
* 2
* conn.createStmtement()
* 3
* 3.1
* 3.2
* 3.3
* 4
* 5
* stmt.executeQuery(sql);
* 6
l Java
l
JDBC
n
n
package com.qianfeng.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/ * *
* JDBC
*
* @author dushine
* /
public class JDBCUtil {
/ * *
*
* @return Connection conn
* @throws SQLException
* /
public static Connection getConnection() throws SQLException {
String url = “jdbc:mysql://localhost:3306/class? useSSL=false”;
String user = “root”;
String password = “root”;
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
}
/ * *
*
* @param conn
* @throws SQLException
* /
public static void releaseSourse(Connection conn) throws SQLException {
if (conn ! = null) {
conn.close();
}
}
/ * *
*
* @param conn
* @param stmt
* @throws SQLException
* /
public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {
if (stmt ! = null) {
stmt.close();
}
if (conn ! = null) {
conn.close();
}
}
/ * *
*
* @param conn
* @param stmt
* @param resultSet
* @throws SQLException
* /
public static void releaseSourse(Connection conn,Statement stmt,ResultSet resultSet) throws SQLException {
if (resultSet ! = null) {
resultSet.close();
}
if (stmt ! = null) {
stmt.close();
}
if (conn ! = null) {
conn.close();
}
}
}
package com.qianfeng.demos;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import com.qianfeng.util.JDBCUtil;
public class Demo04 {
public static void main(String[] args) throws Exception {
/ * *
*
*
*
* /
Scanner sc = new Scanner(System.in);
System.out.println(“
String name = sc.nextLine();
System.out.println(“
String pwd = sc.nextLine();
//
Class.forName(“com.mysql.jdbc.Driver”);
/ *
String url = “jdbc:mysql://localhost:3306/class? useSSL=false”;
String user = “root”;
String password = “root”;
//
Connection conn = DriverManager.getConnection(url, user, password); * /
Connection conn = JDBCUtil.getConnection();
//
Statement stmt = conn.createStatement();
//
String sql = “select * from userinfo where username='”+name+”‘ and password='”+pwd+”‘”;
System.out.println(sql);
//
ResultSet resultSet = stmt.executeQuery(sql);
if (resultSet.next()) {
System.out.println(“
} else {
System.out.println(“
}
JDBCUtil.releaseSourse(conn, stmt, resultSet);
sc.close();
}
}