登录,注册
页面
package com.oracle.view; import java.util.Scanner; import com.oracle.controller.UserController; public class UserView { //登录 private UserController userController=new UserController(); public void login(){ Scanner sc=new Scanner(System.in); System.out.println("请输入用户名:"); String uname=sc.next(); System.out.println("请输入密码:"); String pwd=sc.next(); //调用UserController的登录方法 int row=userController.login(uname, pwd); if(row>0){ System.out.println("登录成功"); new SortView().menu(); }else{ System.out.println("用户名或密码错误"); } } public void zhuce(){ Scanner sc=new Scanner(System.in); System.out.println("请输入姓名:"); String uname=sc.next(); System.out.println("请输入密码(三位数字):"); String pwd=sc.next(); String mima="[0-9]{3}"; String name="[a-z]{3,8}"; if(uname.matches(name)&&pwd.matches(mima)){ int row=userController.chachong(uname, pwd); if(row>0){ System.out.println("注册成功"); }else{ System.out.println("您已经注册过了"); } }else{ System.out.println("你输入的格式有误"); } } }
封装
package com.oracle.controller; import com.oracle.domain.Sort; import com.oracle.service.UserService; public class UserController { private UserService userService=new UserService(); //登录 public int login(String uname,String pwd){ return userService.login(uname, pwd); } public int chachong(String uname,String pwd){ int row=userService.chachong(uname, pwd); return row; } }
条件
package com.oracle.service; import java.sql.SQLException; import com.oracle.dao.UserDao; import com.oracle.tools.JDBCUtils; public class UserService { private UserDao userDao=new UserDao(); public int login(String uname,String pwd){ int row=0; try { row=userDao.login(uname, pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return row; } public int chachong(String uname,String pwd){ int row=0; int rs=0; try { row=userDao.chachong(uname); if(row==0){ rs=userDao.zhuce(uname, pwd); return rs; }else{ return rs=0; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs=0; } }
连接数据库
package com.oracle.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.swing.JButton; import com.oracle.tools.JDBCUtils; public class UserDao { //登录 public int login(String uname,String pwd) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=? and pwd=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); pst.setString(2, pwd); ResultSet rs=pst.executeQuery(); int row=0; while(rs.next()){ row=rs.getInt(1); } return row; } public int chachong(String uname) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="select count(*) from user where uname=?"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); ResultSet rs=pst.executeQuery(); int row=0; while(rs.next()){ row=rs.getInt(1); } return row; } public int zhuce(String uname,String pwd) throws SQLException{ Connection conn=JDBCUtils.getConn(); String sql="insert into user(uname,pwd) values(?,?)"; PreparedStatement pst=conn.prepareStatement(sql); pst.setString(1, uname); pst.setString(2, pwd); int rs=pst.executeUpdate(); JDBCUtils.close(conn, pst); return rs; } }
测试
package com.oracle.test; import com.oracle.view.MainView; public class Demo { public static void main(String[] args) { new MainView().mainMenu(); } }