import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class T {
public static Connection connection=null;
public static Statement statement=null;
public static ResultSet resultSet=null;
public static void main(String[] args) throws Exception{
while (true) {
for (int i = 3; i >=0 ; i--) {
System.out.println("请输入您的账号:");
String username = new Scanner(System.in).nextLine();
System.out.println("请输入您的密码:");
int password = new Scanner(System.in).nextInt();
UserMassege user1 = login(username, password);
if (user1.getUsername() != null && user1.getPassword() != 0) {
System.out.println("欢迎" + user1.getUsername() + "登录系统!!");
return;
} else {
if (i==1){
System.out.println("您的输入次数以用完!!傻逼滚!");
return;
}else {
System.out.println("输入错误,请重新输入!您还剩下"+(i-1)+"次。");
}
}
}
}
}
/**
* 已经封装的登录方法。。。
*
* @param username
* @param password
* @return
* @throws Exception
*/
public static UserMassege login(String username,int password) throws Exception{
Class.forName(JDBCTools.driverClassName);
connection=JDBCTools.getConnection();
statement=connection.createStatement();
String sql="SELECT * from tb_kk WHERE username='"+username+"' and password="+password+"";
resultSet=statement.executeQuery(sql);
UserMassege user=new UserMassege();
while (resultSet.next()){
String username1=resultSet.getString("username");
int password1=resultSet.getInt("password");
user.setUsername(username1);
user.setPassword(password1);
}
//System.out.println(user);
JDBCTools.close(resultSet,statement,connection);
return user;
}
}
public class UserMassege {
private String username;
private int password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
@Override
public String toString() {
return "UserMassege{" +
"username='" + username + '\'' +
", password=" + password +
'}';
}
}
import java.sql.*;
/**
* JDBC工具类封装
*
* @author Joker
* @version 1.0
*
*/
public class JDBCTools {
//驱动类的名称
public static String driverClassName="com.mysql.jdbc.Driver";
public static String url="jdbc:mysql://localhost:3306/mydb1";
public static String user="root";
public static String password="123";
static {
try {
Class.forName(JDBCTools.driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 该方法返回JDBC connection 对象 封装
*
* @return
*/
public static Connection getConnection() throws Exception {
Connection con=null;
try {
con=DriverManager.getConnection(JDBCTools.url, JDBCTools.user, JDBCTools.password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 释放资源封装
*
*/
public static void close(ResultSet rs, Statement sm,Connection cn) {
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (sm!=null){
try {
sm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (cn!=null){
try {
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}