1 创建javaweb常规目录
2 创建pojo实体类
详细代码:
package com.zgl.pojo; public class Users { private int userid; private String username; private String userpwd; private String usersex; private String phonenumber; private String qqnumber; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } public String getUsersex() { return usersex; } public void setUsersex(String usersex) { this.usersex = usersex; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } public String getQqnumber() { return qqnumber; } public void setQqnumber(String qqnumber) { this.qqnumber = qqnumber; } }
3 创建JDBC工具类
3.1 配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/zgl?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
3.2 JDBC工具类
package com.zgl.commons; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ResourceBundle; public class JdbcUtils { private static String driver; private static String url; private static String username; private static String passward; static { ResourceBundle bundle = ResourceBundle.getBundle("db"); driver = bundle.getString("jdbc.driver"); url = bundle.getString("jdbc.url"); username = bundle.getString("jdbc.username"); passward = bundle.getString("jdbc.password"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } //获取链接方法 } public static Connection getConnection() { Connection conn=null; try { conn=DriverManager.getConnection(url,username,passward); } catch (SQLException e) { e.printStackTrace(); } return conn; } //关闭链接 public static void closeConnection(Connection conn){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }