package Dao;
import java.sql.*;
public class User {
	private Connection con=null;
	private Statement st=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	public final static String driver="com.mysql.jdbc.Driver";
	public final static String uri="jdbc:mysql://localhost:3306/News";
	public final static String name="root";
	public final static String password="123456";
	
	//建立数据库连接
	public void getConn(){
		try{
			Class.forName(driver); //加载驱动类
			con=DriverManager.getConnection(uri, name, password);
			System.out.println("数据库已连接");
		}
		catch (ClassNotFoundException e) {
			// TODO: handle exception
			System.out.println("驱动类未找到"+e.toString());
		}
		catch (SQLException e) {
			// TODO: handle exception
			System.out.println("连接对象创建失败"+e.toString());
		}
	}
	
	//进行数据比对
	 public boolean findNamePassword(String name,String pwd){
		 boolean flag=false;
		 String sql="select * from User where name=? and pwd=?";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setString(1,name);
			 ps.setString(2, pwd);
			 rs=ps.executeQuery();
			 if (rs.next()) {
				System.out.println("用户存在");
				flag=true;
			}
			 else{
				 System.out.println("用户不存在");
				 flag=false;
			 }
		 }
		 catch (SQLException e) {
			// TODO: handle exception
			 e.printStackTrace();
		}
		 return flag;
	 }
	 
	 //获取用户名
	 public boolean findName(String name){
		 boolean flag=false;
		 String sql="select * from User where name=?";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setString(1,name);
			 rs=ps.executeQuery();
			 if(rs.next()){
				 System.out.println("用户存在");
				 flag=true;
			 }
			 else{
				 System.out.println("用户不存在");
				 flag=false;
			 }
		 }
		 catch (SQLException e) {
				// TODO: handle exception
				 e.printStackTrace();
			}
			 return flag;
		   
	 }
	 
	 //查询用户编号
	 public boolean findID(int uid){
		 boolean flag=false;
		 String sql="select * from User where uid=?";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setInt(1,uid);
			 rs=ps.executeQuery();
			 if(rs.next()){
				 System.out.println("uid存在");
				 flag=true;
			 }
			 else{
				 System.out.println("uid不存在");
				 flag=false;
			 }
		 }
		 catch (SQLException e) {
				// TODO: handle exception
				 e.printStackTrace();
			}
			 return flag;
		   
	 }
	 
	 //用户注册
	 public int insertUser(String name,String pwd,String mail){
		 int i=0;
		 String sql="insert into User(name,pwd,mail)values(?,?,?)";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setString(1, name);
			 ps.setString(2, pwd);
			 ps.setString(3, mail);
			 i=ps.executeUpdate();
		 }
		 catch (SQLException e) {
			// TODO: handle exception
			 e.printStackTrace();
		}
		 return i;
	 }
	 
	 //用户注销
	 public void delete(int uid){
		 String sql="delete from User where uid=? ";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setInt(1, uid);
			 ps.executeUpdate();
		 }
		 catch (SQLException e) {
			// TODO: handle exception
			 e.printStackTrace();
		} 
	 }
	 //用户信息更新
	 public void update(String name,String pwd){
		 String sql="update User set pwd=? where name=?";
		 try{
			 ps=con.prepareStatement(sql);
			 ps.setString(1, pwd);
			 ps.setString(2, name);
			 ps.executeUpdate();
			 System.out.println("信息更新成功");
		 }
		 catch (SQLException e) {
			// TODO: handle exception
			 e.printStackTrace();
		} 
	 }
}
最近下载更多
Waner111  LV1 1月4日
asddwh  LV12 2023年12月26日
zj20020510  LV5 2023年9月24日
monolog  LV4 2023年4月18日
whq12138  LV3 2023年1月6日
北方菜  LV11 2022年12月30日
1721281527  LV2 2022年12月27日
include  LV8 2022年12月22日
1379585889  LV11 2022年12月13日
13169178967  LV2 2022年12月11日
最近浏览更多
op123129 4月14日
暂无贡献等级
Waner111  LV1 1月4日
asddwh  LV12 2023年12月26日
微信网友_6786215447367680  LV4 2023年12月23日
颜菜菜  LV2 2023年12月23日
微信网友_6783897029726208 2023年12月18日
暂无贡献等级
lf3203119970 2023年11月30日
暂无贡献等级
蹇金金  LV7 2023年11月6日
graceful 2023年10月18日
暂无贡献等级
微信网友_6680567232876544  LV8 2023年10月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友