做网站赚钱吗 怎么赚钱,wordpress图片类主题,注册网站会员需填写,建筑设计装修一、工具类及配置文件准备工作1.1 引入jar包使用原生MySQL#xff0c;只需要用到MySQL连接的jar包#xff0c;maven引用方式如下#xff1a;mysqlmysql-connector-java5.1.481.2 jdbc.properties文件配置在resources文件夹根目录#xff0c;新增jdbc.properties配置文件只需要用到MySQL连接的jar包maven引用方式如下mysqlmysql-connector-java5.1.481.2 jdbc.properties文件配置在resources文件夹根目录新增jdbc.properties配置文件内容如下drivercom.mysql.jdbc.Driverurljdbc:mysql://localhost:3306/mydbuserrootpassword1234561.3 JDBCUtils工具类在java文件夹中新增 util -- JDBCUtils.java 类该类中获取jdbc.properties中的值。JDBCUtils工具类主要作用是简化获取MySQL配置文件、关闭资源。private staticString url;private staticString user;private staticString password;static{Properties properties newProperties();try{properties.load(Mytest.class.getClassLoader().getResourceAsStream(jdbc.properties));url properties.getProperty(url);user properties.getProperty(user);password properties.getProperty(password);Class.forName(properties.getProperty(driver));}catch (IOException |ClassNotFoundException e) {e.printStackTrace();}}//1.获取jdbc.properties配置文件中的数据库连接public static Connection getConnection() throwsSQLException {returnDriverManager.getConnection(url, user, password);}//5.定义关闭资源的方法public static voidclose(Connection conn, Statement stmt, ResultSet rs) {if (rs ! null) {try{rs.close();}catch(SQLException e) {}}if (stmt ! null) {try{stmt.close();}catch(SQLException e) {}}if (conn ! null) {try{conn.close();}catch(SQLException e) {}}}public static voidclose(Connection conn, Statement stmt) {close(conn, stmt,null);}二、原生MySQL实现增删改查2.1 语法说明1、通过Connection获取数据库连接对象2、定义sql语句(一般可以在Navicat中直接执行)3、通过获取执行sql的对象 --PreparedStatement4、执行sql语句增删改使用conn的executeUpdate方法(成功返回值为int1)查询使用executeQuery方法(返回值为ResultSet建议使用下文的查询方法操作)5、释放资源(执行SQL时定义的stmt、获取连接时的conn)。2.2 新增数据 -- insertUser()在java文件夹中新增MyService.java类将获取数据库连接抽取出来方法如下privateConnection conn;{try{connJDBCUtils.getConnection();}catch(SQLException e) {e.printStackTrace();}}在MyService.java类中新增 insertUser方法具体如下String sql INSERT INTO user values (4, 腾讯科技, xinfeng37812, 2009-11-16, 广东省深圳市);PreparedStatement stmtconn.prepareStatement(sql);int count stmt.executeUpdate(sql);JDBCUtils.close(conn, stmt);return count;2.2 修改数据 -- updateById()String sql update user set password 567875 where id 2;PreparedStatement stmtconn.prepareStatement(sql);int count stmt.executeUpdate(sql);JDBCUtils.close(conn, stmt);return count;2.3 删除数据 -- deleteUser()String sql delete from user where id 5;PreparedStatement stmtconn.prepareStatement(sql);int count stmt.executeUpdate(sql);JDBCUtils.close(conn, stmt);return count;2.4 查询数据 -- findAll()前提新建 entity -- User.java 实体类并获取gettersetter、toSting方法String sql select * from user;PreparedStatement stmtconn.prepareStatement(sql);ResultSet countstmt.executeQuery(sql);User user null;List arr new ArrayList();while(count.next()){Long id count.getLong(id);String username count.getString(username);String password count.getString(password);Date birthday count.getDate(birthday);String address count.getString(address);user newUser();user.setId(id);user.setUsername(username);user.setPassword(password);user.setBirthday(birthday);user.setAddress(address);arr.add(user);}JDBCUtils.close(conn, stmt, count);return arr;三、原生MySQL语句的缺点及数据库连接池3.1 原生MySQL语句的缺点1、每一次查询都要新增通道关闭通道效率太低。实际项目中都会用数据库连接池进行优化2、实际项目中使用最多的就是查询但是将查询的ResultSet结果进行封装的代码过于臃肿。3.2 c3p0和druid连接池技术数据库连接池其实就是一个容器在java中使用getConnection方法代替Connection实现节约资源用户访问高效目的但是代码本身与原生并无本质的减少。3.2.1 c3p0使用需要导入两个jar包maven引用方式如下com.mchangec3p00.9.5.5com.mchangemchange-commons-java0.2.15配置文件必须在resources文件夹根目录且名称必须为 c3p0.properties 或者 c3p0-config.xml因此无需手动加载配置文件//1.创建数据库连接池对象DataSource ds newComboPooledDataSource();//2. 获取连接对象Connection conn ds.getConnection();3.2.2 druid使用只需要一个jar包maven引入方式如下com.alibabadruid-spring-boot-starter1.1.9配置文件名称任意但需要是.properties形式的因此需要获取配置文件位置具体使用方式如下//1.加载配置文件Properties pro newProperties();InputStream is DruidDemo.class.getClassLoader().getResourceAsStream(druid.properties);pro.load(is);//2.获取连接池对象DataSource ds DruidDataSourceFactory.createDataSource(pro);//3.获取连接Connection conn ds.getConnection();3.3 JDBCUtils工具类的改造以使用druid为例在使用数据库连接池时的工具类主要有三种方法1. 获取连接方法通过数据库连接池获取连接2. 释放资源3. 获取连接池的方法public classJDBCUtils {private staticDataSource ds;static{try{Properties pro newProperties();pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream(druid.properties));dsDruidDataSourceFactory.createDataSource(pro);}catch(IOException e) {e.printStackTrace();}catch(Exception e) {e.printStackTrace();}}public static Connection getConnection() throwsSQLException {returnds.getConnection();}public static voidclose(Statement stmt, Connection conn) {close(null, stmt, conn);}public static voidclose(ResultSet rs, Statement stmt, Connection conn) {if (rs ! null) {try{rs.close();}catch(SQLException e) {e.printStackTrace();}}if (stmt ! null) {try{stmt.close();}catch(SQLException e) {e.printStackTrace();}}if (conn ! null) {try{conn.close();//归还连接} catch(SQLException e) {e.printStackTrace();}}}public staticDataSource getDataSource() {returnds;}}