用户登录小程序
来源:互联网 发布:乐百视网络机顶盒 编辑:程序博客网 时间:2024/05/01 23:06
在数据库建一个表格存入诺干用户跟用户密码,id自增
程序不长就写在一个class里面啦
里面涉及到的JDBC相关的类跟方法详见上一篇,有详细解释
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;import com.mysql.jdbc.PreparedStatement;public class TestStatement { static Scanner sc=new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入用户名:"); String name=sc.nextLine(); System.out.println("请输入密码:"); String password =sc.nextLine(); User user=isExist(name, password); if(user!=null){ System.out.println("登录成功"); } else{ System.out.println("登录失败"); } } //获取连接 public static Connection getConnection(){ String driver="com.mysql.jdbc.Driver";//mysql的Driver String url="jdbc:mysql://localhost:3306/test";//数据库的位置 String user="root"; String password="123456"; Connection conn=null; try { Class.forName(driver);//加载注册驱动 conn = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } //判断输入的用户名跟密码是否存在 public static User isExist(String name,String password){ String sql="select *from user where name=? and password=?"; Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; User user=null; try { conn = getConnection(); ps = (PreparedStatement) conn.prepareStatement(sql);//PreparedStatement对象用于执行sql语句 ps.setString(1, name);//给占位符?赋值 ps.setString(2, password);//给占位符?赋值 rs = ps.executeQuery();//结果集对象 while(rs.next()){ user=new User(); user.setId(rs.getInt(1)); user.setName(rs.getString(2)); user.setPassword(rs.getString(3)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } return user; }}//用户类class User{ private int id; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + "]"; } public User(int id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public User() { }}
结果如下
输入正确与不正确时:
0 0
- 用户登录小程序
- python-用户登录小程序
- 一个汇编小程序:用户登录验证程序
- 程序模拟用户登录
- 用户登录程序实现
- 简单用户登录程序
- 小程序登录步骤
- 用户WEB登录小例子
- struts2小例子 登录用户
- 用户登录界面小实例
- Java小程序之集合框架模拟数据库实现用户登录和注册功能
- Java小程序之集合框架模拟数据库实现用户登录和注册功能
- [J2SE]Java登录小程序
- 登录小程序bug总结
- struts2登录验证小程序
- 微信端开发--登录小程序
- 小程序开发之登录
- 小程序微信登录
- 安卓ListView的Item中含有EditText,EditText里面的值保存,并且滑动ListView时EditText值不改变
- Android中利用Handler做的倒计时
- pentaho Report Designer 入门教程(二)
- UDP穿透NAT Firewall原理
- PriorityQueue剖析
- 用户登录小程序
- 适配器模式(转)
- The SetStack Computer(stack) -uva 12096
- pentaho Report Designer 入门教程(三)
- class字节计算
- mybatis, mapper.xml常用配置
- 【Linux开发】Linux V4L2驱动架构解析与开发导引
- Floyd判圈算法
- 【J2SE】——初见Java