JDBC模拟登陆

package com.oracle.demo01;

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

public class Demo08 {
    public static void main(String[] args){

        //获得预处理对象
        String sql="select count(*) from user where uname=? and pwd=?";
        PreparedStatement pst=null;
        try {
            //调用JDBCUtils.getconn()-->自定义类
            pst = JDBCUtils.getconn().prepareStatement(sql);
        } catch (SQLException e1) {
            
            e1.printStackTrace();
        }
        
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name=sc.next();
        System.out.println("请输入密码:");
        String pwd=sc.next();
        
        try {
            //执行查询语句
            pst.setString(1, name);
            pst.setString(2, pwd);
            ResultSet rs=pst.executeQuery();
            //查看是否成功
            int count=0;
            while (rs.next()) {
                count=rs.getInt(1);
            }
            if (count>0) {
                System.out.println("登陆成功");
            }else{
                System.out.println("登陆失败");
            }
            
        } catch (SQLException e) {
            
            e.printStackTrace();
        }finally {
            //释放资源
            try {
                sc.close();
                pst.close();
                
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
        }
    }
}
                

.

猜你喜欢

转载自www.cnblogs.com/l1314/p/12148126.html