java preparedStatement 查询案例

package jdbc.Test;

import jdbc.Utils.jdbc_Utils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class jdbc_login {

    public static void main(String args[]) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();

        String sql = "select * from user where username =? and password =?";

        Connection connection = jdbc_Utils.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);

        ResultSet resultSet = preparedStatement.executeQuery();

        boolean isval = false;

        if (resultSet.next()){
            isval = true;
        }

        jdbc_Utils.closeAll(resultSet,preparedStatement,connection);

        if (isval){
            System.out.println("登陆成功!");
        }else {
            System.out.println("登录失败!");
        }

    }
}
 

你可能感兴趣的:(java,开发语言)