使用JDBC实现javaweb登录界面

目录

使用JDBC实现javaweb登录界面

题目要求

1.表的创建

2.使用jdbc

jdbc的配置文件

文件层级展示

3.登录界面

4.user的实体类


使用JDBC实现javaweb登录界面

题目要求

 

1.表的创建

use soso;
create table user(
    id int primary key auto_increment,
 username varchar(32),
 password varchar(32)
);
​
insert into user values(null,'zhangsan','123');
insert into user values(null,'lisi','234');
​

2.使用jdbc

/**
 * jdbc的工具类
 */
public class JDBCUtil {
    static Properties props = new Properties();
    static{
        InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            props.load(in);
            Class.forName(props.getProperty("driverClass"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //得到连接的方法
    public static Connection getConnection() throws SQLException {
        return  DriverManager.getConnection(props.getProperty("url"),props.getProperty("userName"),props.getProperty("password"));
    }
​
    //关闭资源的方法
    public static void close(ResultSet rs, PreparedStatement pstmt,Connection conn ){
        try {
            if(rs != null) rs.close();
            if(pstmt != null) pstmt.close();
            if(conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
​
    /**
     * 执行增删改的sql
     */
    public static int executeUpdate(String sql,Object...params){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtil.getConnection();
            pstmt = createPreparedStatement(conn,sql,params);
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(null,pstmt,conn);
        }
        return 0;
    }
​
    private static  PreparedStatement  createPreparedStatement(Connection conn,String sql,Obj
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值