当前位置:   article > 正文

【Sql Server】eclipse 连接sql server实现登录注册功能_eclipse实现注册登录

eclipse实现注册登录
  1. package app;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7. import java.sql.*;
  8. import game.GameFrame;
  9. //程序入口
  10. //改名快捷键shift +f6
  11. public class GameMain {
  12. static Scanner sc = new Scanner(System.in);
  13. static String name;
  14. static String passward;
  15. public static void main(String[] args) {
  16. System.out.println("***********欢迎来到坦克大战***********");
  17. login();
  18. new GameFrame();
  19. }
  20. public static void login() {
  21. System.out.println("**********请选择你的操作:**********");
  22. System.out.println("1.登录 2.注册");
  23. int n = sc.nextInt();
  24. if (n == 1) {
  25. System.out.println("请输入昵称:");
  26. name = sc.next();
  27. System.out.println("请输入密码:");
  28. passward = sc.next();
  29. String sql = "select * from Users where admin='" + name + "'and password='" + passward + "'";
  30. if (flogin(sql)) {
  31. System.out.println("**********登录成功!**********");
  32. } else {
  33. System.out.println("**********用户不存在!**********");
  34. login();
  35. }
  36. } else if (n == 2) {
  37. String str;
  38. System.out.println("请输入昵称:");
  39. name = sc.next();
  40. System.out.println("请输入密码:");
  41. passward = sc.next();
  42. System.out.println("确认密码:");
  43. str = sc.next();
  44. while (!passward.equals(str)) {
  45. System.out.println("**********密码输入不一致,请重新输入**********");
  46. System.out.println("请输入密码:");
  47. passward = sc.next();
  48. System.out.println("确认密码:");
  49. str = sc.next();
  50. }
  51. if (register(name, passward)) {
  52. System.out.println("**********注册成功!**********");
  53. } else {
  54. System.out.println("**********注册失败!请重新注册**********");
  55. }
  56. login();
  57. } else {
  58. System.out.println("暂无该操作");
  59. login();
  60. }
  61. }
  62. // 数据库连接
  63. static Connection con;
  64. public static Connection getconnection() {
  65. String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  66. String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TanKe";// TanKe为数据库名
  67. String userName = "sa";// 你的数据库用户名
  68. String userPwd = "20020319";// 你的密码
  69. try {
  70. Class.forName(driverName);
  71. System.out.println("加载驱动成功!");
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. System.out.println("加载驱动失败!");
  75. }
  76. try {
  77. con = DriverManager.getConnection(dbURL, userName, userPwd);
  78. Statement statement = null;
  79. ResultSet rs = null;
  80. System.out.println("连接数据库成功!");
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. System.out.print("SQL Server连接失败!");
  84. }
  85. return con;
  86. }
  87. // 登录方法
  88. public static boolean flogin(String sql) {
  89. Connection connection = getconnection();
  90. try {
  91. Statement statement = (Statement) connection.createStatement();
  92. ResultSet rs = statement.executeQuery(sql);
  93. if (rs.next()) {
  94. //打印用户
  95. // while (rs.next()) {
  96. // int id = rs.getInt("ID");
  97. // String userName = rs.getString("admin");
  98. // int userage = rs.getInt("password");
  99. // System.out.print(id + " ");
  100. // System.out.print(userName + " ");
  101. // System.out.println(userage);
  102. // }
  103. statement.close();
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. } catch (SQLException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. return false;
  112. }
  113. }
  114. // 注册方法
  115. public static boolean register(String name, String passward) {
  116. Connection connection = getconnection();
  117. String sql = "select * from Users where admin='" + name + "'";
  118. try {
  119. Statement statement = (Statement) connection.createStatement();
  120. ResultSet rs = statement.executeQuery(sql);
  121. if (rs.next()) {
  122. System.out.println("用户名已存在!");
  123. return false;
  124. } else {
  125. String updateSql = "insert into Users values(?,?);";
  126. PreparedStatement presta = connection.prepareStatement(updateSql);
  127. presta.setString(1, name);
  128. presta.setString(2, passward);
  129. presta.executeUpdate();
  130. statement.close();
  131. presta.close();
  132. return true;
  133. }
  134. } catch (SQLException e) {
  135. // TODO Auto-generated catch block
  136. e.printStackTrace();
  137. return false;
  138. }
  139. }
  140. // 释放数据库
  141. public void deleteConnection() throws SQLException {
  142. if (con != null) {
  143. con.close();
  144. }
  145. con = null;
  146. }
  147. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/687357
推荐阅读
相关标签
  

闽ICP备14008679号