jdbc(二)

参考:https://www.runoob.com/w3cnote/jdbc-use-guide.html

DbUtil

即jdbc工具类,用于提供Connection对象

public class DbUtil {
    public static final String URL = "jdbc:mysql://localhost:3306/imooc";
    public static final String USER = "liulx";
    public static final String PASSWORD = "123456";
    private static Connection conn = null;
    static{
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }
}

Dao类

void add(User user)throws SQLException{
      Connection conn = DbUtils.getConnection();
      //...
}

jdbc(二)

上一篇:ansible之资源清单


下一篇:element ui 更改无数据提示信息