package abc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class jdbc1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
load(username, password);
}
public static boolean load(String username, String password) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Student");
while (rs.next()) {
if (rs.getString("username").equals(username) && rs.getString("password").equals(password)) {
System.out.println("登录成功");
if (rs != null)
rs.close();
if (st != null)
st.close();
if (con != null)
con.close();
return true;
}
}
System.out.println("登录失败");
if (rs != null)
rs.close();
if (st != null)
st.close();
if (con != null)
con.close();
return false;
}
} |