JDBC用户名和密码登录

package yt052101;


import java.security.interfaces.RSAKey;
import java.sql.*;
import java.util.*;

public class JDBC {
    

	public static void main(String[] args) {
		List<Employee> list = fetchDate();
		for(Employee e:list){
			System.out.println(e);
		}
		Employee employee =login("yun", "ffh1");
		if(employee!=null){
			System.out.println("登陆成功!欢迎"+employee.getDnama());
		}else{
			System.out.println("登录失败,请重新登录");
		}
	}
	
	private static Employee login(String dname,String dcount){
		Employee employee=null;
		Connection conn=null;
		try {
			Class.forName("org.gjt.mm.mysql.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yun","root","123456");
			PreparedStatement ps=conn.prepareStatement("select * from dvds where dname=? and dcount=?");
			ps.setString(1, dname);
			ps.setString(2, dcount);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				employee = new Employee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return employee;
		
		
	}

	private static ArrayList<Employee> fetchDate() {
		ArrayList<Employee> list=new ArrayList<Employee>();
	    Employee employee =null;
	    Connection conn=null;
	    //加载驱动程序
	    try {
			Class.forName("org.gjt.mm.mysql.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yun","root","123456");
			Statement sm=conn.createStatement();
			String string="select * from dvds";
			ResultSet rs=sm.executeQuery(string);
			while(rs.next()){
				employee=new Employee(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				list.add(employee);
			}
			
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	    return list;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值