张迪d

导航

统计

作业

题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id、username、password),验证登录是否成功。

题目2:在上一题基础上,当登录成功后,将t_user表(id、name、sex、birthday)的信息进行显示(要求使用DB.java完成登录和获取t_user表中数据的操作),最后再对t_user表进行一条记录的添加操作。

DB

package demo;
import java.sql.*;
public class DB {
	    private Connection con;
	    private PreparedStatement pre;
	    private ResultSet rs;
	    private static DB db;
	    static {
	        try {
	            Class.forName("com.mysql.jdbc.Driver");
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        }
	    }
	    public DB() {
	        try {
	            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","082256");
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }
	    public static DB getInstance() {
	        if(db == null) {
	            db = new DB();
	        }
	        return db;
	    }
	    public ResultSet executeSelect (String sql, Object[] args) {
	        try {
	            pre = con.prepareStatement(sql);
	            if(args.length !=0) {
	                for(int i = 0;i< args.length;i++) {
	                    pre.setObject(i+1, args[i]);
	                }
	            }
	            rs = pre.executeQuery();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        return rs;
	    }
	    public int executeModify (String sql, Object[] args) {
	        int n = 0;
	        try {
	            pre = con.prepareStatement(sql);
	            if(args.length !=0) {
	                for(int i = 0;i< args.length;i++) {
	                    pre.setObject(i+1, args[i]);
	                }
	            }
	            n = pre.executeUpdate();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	        return n;
	    }
	    public void close() {
	        try {
	            if (rs != null) {
	                rs.close();
	            }
	            pre.close();
	            con.close();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
	    }
	    
	}

  text

package demo;

import java.sql.*;
import java.util.Scanner;
public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		String username=scan.next();
		String password=scan.next();
		Connection con=null;
		Statement st=null;
		ResultSet rs=null;
		ResultSet rs1=null;
		try {
			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","082256");
			st=con.createStatement();

			 String sql = "select * from t_login where username='" + username+ "' and password = '" + password + "'";
	         rs=st.executeQuery(sql);
	         boolean bl=rs.next();
	         if(bl) {
	        	 System.out.println("登陆成功");
	        	rs1= DB.getInstance().executeSelect("select * from t_user", args);
	         	while(rs1.next()) {
	         		int id = rs1.getInt(1);
                    String name = rs1.getString(2);
                    String sex = rs1.getString(3);
                    String birthday = rs1.getString(4);
                    System.out.println("id     name     sex      birthday");
                    System.out.println(id + "           " + name + "         " + sex + "            " + birthday);
	         	}
	         	System.out.println("添加信息");
	         	int id=scan.nextInt();
	         	String name=scan.next();
	         	String sex=scan.next();
	         	String birthday=scan.next();
	         	String sql1="insert into t_user(id,name,sex,birthday) values('"+id+"','"+name+"','"+sex+"','"+birthday+"')";
	         	int count=DB.getInstance().executeModify(sql1, args);
	         	if(count>0) 
	         		System.out.println("数据添加成功");
	         	else
	         		System.out.println("数据添加失败");
	         }
	         else {
	        	 System.out.println("登陆失败");
	         }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

 

 

 

 

 

posted on 2019-12-12 21:01  张迪d  阅读(...)  评论(...)    收藏