采用Model1(jsp+javabean)
- 实现DBHelper类
- 创建实体类
创建业务逻辑类(dao)
DBHelper类的设计
package util;import java.sql.Connection;
import java.sql.DriverManager;public class DBHelper {private static final String driver = "com.mysql.jdbc.Driver"; //数据库驱动//连接数据库的URL地址private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8"; private static final String username="root";//数据库的用户名private static final String password="";//数据库的密码private static Connection conn=null;//静态代码块负责加载驱动static {try{Class.forName(driver);}catch(Exception ex){ex.printStackTrace();}}//单例模式返回数据库连接对象public static Connection getConnection() throws Exception{if(conn==null){conn = DriverManager.getConnection(url, username, password);return conn;}return conn;}public static void main(String[] args) {try{Connection conn = DBHelper.getConnection();if(conn!=null){System.out.println("数据库连接正常!");}else{System.out.println("数据库连接异常!");}}catch(Exception ex){ex.printStackTrace();}}
}