目录
使用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