赞
踩
- package app;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Scanner;
- import java.sql.*;
-
- import game.GameFrame;
-
- //程序入口
- //改名快捷键shift +f6
- public class GameMain {
- static Scanner sc = new Scanner(System.in);
- static String name;
- static String passward;
-
- public static void main(String[] args) {
-
- System.out.println("***********欢迎来到坦克大战***********");
- login();
- new GameFrame();
- }
-
- public static void login() {
- System.out.println("**********请选择你的操作:**********");
- System.out.println("1.登录 2.注册");
- int n = sc.nextInt();
- if (n == 1) {
- System.out.println("请输入昵称:");
- name = sc.next();
- System.out.println("请输入密码:");
- passward = sc.next();
- String sql = "select * from Users where admin='" + name + "'and password='" + passward + "'";
- if (flogin(sql)) {
- System.out.println("**********登录成功!**********");
- } else {
- System.out.println("**********用户不存在!**********");
- login();
- }
- } else if (n == 2) {
- String str;
- System.out.println("请输入昵称:");
- name = sc.next();
- System.out.println("请输入密码:");
- passward = sc.next();
- System.out.println("确认密码:");
- str = sc.next();
- while (!passward.equals(str)) {
- System.out.println("**********密码输入不一致,请重新输入**********");
- System.out.println("请输入密码:");
- passward = sc.next();
- System.out.println("确认密码:");
- str = sc.next();
- }
- if (register(name, passward)) {
- System.out.println("**********注册成功!**********");
- } else {
- System.out.println("**********注册失败!请重新注册**********");
- }
- login();
-
- } else {
- System.out.println("暂无该操作");
- login();
- }
- }
-
- // 数据库连接
- static Connection con;
-
- public static Connection getconnection() {
- String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TanKe";// TanKe为数据库名
- String userName = "sa";// 你的数据库用户名
- String userPwd = "20020319";// 你的密码
- try {
- Class.forName(driverName);
- System.out.println("加载驱动成功!");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("加载驱动失败!");
- }
- try {
- con = DriverManager.getConnection(dbURL, userName, userPwd);
- Statement statement = null;
- ResultSet rs = null;
- System.out.println("连接数据库成功!");
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print("SQL Server连接失败!");
- }
- return con;
- }
-
- // 登录方法
- public static boolean flogin(String sql) {
- Connection connection = getconnection();
-
- try {
- Statement statement = (Statement) connection.createStatement();
- ResultSet rs = statement.executeQuery(sql);
- if (rs.next()) {
- //打印用户
- // while (rs.next()) {
- // int id = rs.getInt("ID");
- // String userName = rs.getString("admin");
- // int userage = rs.getInt("password");
- // System.out.print(id + " ");
- // System.out.print(userName + " ");
- // System.out.println(userage);
- // }
- statement.close();
- return true;
- } else {
- return false;
- }
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
-
- }
-
- // 注册方法
- public static boolean register(String name, String passward) {
- Connection connection = getconnection();
- String sql = "select * from Users where admin='" + name + "'";
- try {
- Statement statement = (Statement) connection.createStatement();
- ResultSet rs = statement.executeQuery(sql);
- if (rs.next()) {
- System.out.println("用户名已存在!");
- return false;
- } else {
- String updateSql = "insert into Users values(?,?);";
- PreparedStatement presta = connection.prepareStatement(updateSql);
- presta.setString(1, name);
- presta.setString(2, passward);
- presta.executeUpdate();
- statement.close();
- presta.close();
- return true;
- }
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
-
- // 释放数据库
- public void deleteConnection() throws SQLException {
- if (con != null) {
- con.close();
- }
- con = null;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。