⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 const.java

📁 简易银行卡管理系统作为ATM的模拟系统
💻 JAVA
字号:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Const {
	private static final String drivername="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=CZLBANK";
	private static final String u="czl";
	private static final String p="123";
	static{
		try {
			Class.forName(drivername);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
	}
	public static Connection getConnection(){
		try {
			return DriverManager.getConnection(url,u,p);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return null;
	}
	public static long getAccount(){
		Connection conn=getConnection();
		if(conn==null)return -1L;
		try {
			Statement stat=conn.createStatement();
			ResultSet rs=stat.executeQuery("select max(account) from userInfo");
			String sid=null;
			long id=0L;
			while(rs.next()){
				sid=rs.getString(1);
			}
			if(sid==null)return -1L;
			id=Long.parseLong(sid);
			rs.close();
			stat.close();
			conn.close();
			return id;
		} catch (SQLException e) {
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return -1L;
	}
	public static boolean newAccount(User user){
		Connection conn=getConnection();
		if(conn==null)return false;
		try{
			Statement stat=conn.createStatement();
			stat.execute("insert into userInfo values('"+
					user.getUsername()+"','"+
					new String(user.getPassword())+"','"+
					user.getAccount()+"','"+
					user.getUserid()+"',"+
					user.getFlag()+","+
					user.getMoney()+");"
					);
			conn.close();
		}
		catch(SQLException e){
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
				return false;
			}
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static User getUser(String username, char[] password) {
		Connection conn=getConnection();
		if(conn==null)return null;
		try{
			Statement stat=conn.createStatement();
			ResultSet rs=stat.executeQuery("select * from userInfo where username='"+username+
					"'and password='"+new String(password)+"';");
			while(rs.next()){
				User user=new User();
				user.setAccount(rs.getString("account"));
				user.setFlag(rs.getInt("flag"));
				user.setMoney(rs.getDouble("money"));
				user.setUserid(rs.getString("userId"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password").toCharArray());
				conn.close();
				return user;
			}
			conn.close();
			return null;
		}
		catch(SQLException e){
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
				return null;
			}
			e.printStackTrace();
			return null;
		}
	}
	public static boolean logout(User u) {
		Connection conn=getConnection();
		if(conn==null)return false;
		try{
			Statement stat=conn.createStatement();
			stat.execute("delete from userInfo where username='"+u.getUsername()
					+"'and password='"+new String(u.getPassword())+"';");
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		}
		return true;
	}
	public static boolean reportLose(User u) {
		Connection conn=getConnection();
		if(conn==null)return false;
		try{
			Statement stat=conn.createStatement();
			stat.execute("update userInfo set flag=1 " +
					"where username='"+u.getUsername()
					+"'and password='"+new String(u.getPassword())+"';");
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		}
		return true;
	}
	public static boolean updateMoney(User u) {
		Connection conn=getConnection();
		if(conn==null)return false;
		try{
			Statement stat=conn.createStatement();
			stat.execute("update userInfo set money='" +u.getMoney()+
					"' where username='"+u.getUsername()
					+"'and password='"+new String(u.getPassword())+"';");
			conn.close();
		}
		catch(SQLException e){
			e.printStackTrace();
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		}
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -