1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package cn.tedu;
public class Demo12 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String username = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); System.out.println("请输入昵称:"); String nickname = sc.nextLine();
try(Connection conn = DBUtils.getConn()) { String sql = "select id from user where username = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,username); ResultSet rs = ps.executeQuery(); if (rs.next()) { System.out.println("用户名已经存在了!"); return; } String insertSql = "insert into user values (null,?,?,?)"; PreparedStatement insertPs = conn.prepareStatement(insertSql); insertPs.setString(1,username); insertPs.setString(2,password); insertPs.setString(3,nickname); insertPs.executeUpdate(); System.out.println("注册成功!"); } catch (SQLException e) { e.printStackTrace(); } } }
|