当前位置: 首页 > news >正文

正常开发一个网站需要多少钱企业网站推广方法实验报告

正常开发一个网站需要多少钱,企业网站推广方法实验报告,好网站开发培训,wordpress audio主题文章目录 0 说明1 如何使用jdbc操作数据库1.1 加载数据库驱动1.2 建立数据库连接1.3 创建Statement或者PreparedStatement用来执行SQL1.4 开始执行SQL语句1.5 处理结果集1.6 关闭连接1.7 完整代码 2 批量操作数据库3 如何打印SQL语句4 jdbc常用开源类库 1 JDBC实现往MySQL插入百…

文章目录

  • 0 说明
  • 1 如何使用jdbc操作数据库
    • 1.1 加载数据库驱动
    • 1.2 建立数据库连接
    • 1.3 创建Statement或者PreparedStatement用来执行SQL
    • 1.4 开始执行SQL语句
    • 1.5 处理结果集
    • 1.6 关闭连接
    • 1.7 完整代码
  • 2 批量操作数据库
  • 3 如何打印SQL语句
  • 4 jdbc常用开源类库

1 JDBC实现往MySQL插入百万级数据
2 详解JDBC(Java Database connect)

0 说明

JDBCJava 数据库连接)是 Java 语言中用来连接和操作关系型数据库的 API。使用 JDBC,我们可以通过编写 Java 代码来访问各种数据库,包括 MySQLOracleSQL Server 等。
JDBC 是基于 Java 的标准接口,它提供了一组接口和类,可以让 Java 应用程序与各个数据库进行交互。JDBC 的工作原理如下:

1.、加载数据库驱动程序

在使用 JDBC 前,需要先加载相应的数据库驱动程序,不同的数据库使用的驱动程序不同。通常情况下,我们需要将驱动程序的 jar 文件添加到项目的类路径中,并在代码中使用 Class.forName() 方法来加载驱动程序。

2、连接数据库

使用 DriverManager 类来连接数据库,该类提供了一组方法来获取数据库连接。连接字符串通常由以下三部分组成:协议、主机名和数据库名称。

3、执行 SQL 语句

连接成功后,就可以通过 Connection 对象创建 Statement 或 PreparedStatement 对象,用于执行 SQL 语句。Statement 支持静态 SQL 语句,而 PreparedStatement 支持动态 SQL 语句。执行 SQL 语句后,可以通过 ResultSet 对象获取查询结果。

4.、关闭连接

完成数据库操作后,必须关闭 Connection、Statement 和 ResultSet 对象,释放资源。

1 如何使用jdbc操作数据库

1.1 加载数据库驱动

在代码中使用 Class.forName() 方法加载数据库驱动程序。


Class.forName("com.mysql.cj.jdbc.Driver");

1.2 建立数据库连接

使用 DriverManager 类的 getConnection() 方法,传入连接字符串、用户名和密码等参数来获取数据库连接。


private String url = "jdbc:mysql://localhost:3306/school";
private String user = "root";
private String password = "root";Connection conn = DriverManager.getConnection(url, user, password);

1.3 创建Statement或者PreparedStatement用来执行SQL

使用 Connection 对象的 createStatement() 或 prepareStatement() 方法创建 Statement 或 PreparedStatement 对象,用于执行 SQL 语句。


String sql = "select * from user limit 1";
PreparedStatement preparedStatement = connection.prepareStatement(sql);

1.4 开始执行SQL语句

使用 Statement 或 PreparedStatement 对象的 executeQuery() 或 executeUpdate() 方法执行 SQL 语句,返回 ResultSet 或更新的行数。


String sql = "select * from user limit 1";
ResultSet rs = preparedStatement .executeQuery(sql);
int count = preparedStatement .executeUpdate();

1.5 处理结果集

使用 ResultSet 对象的 getXXX() 方法获取查询结果,例如 getInt()、getString() 等方法。

while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");int age = rs.getInt("age");
}

1.6 关闭连接

使用 Connection、Statement 和 ResultSet 对象的 close() 方法关闭连接,释放资源。

	rs.close();stmt.close();conn.close();

1.7 完整代码

    @Testpublic void t10() {String url = "jdbc:mysql://localhost:3306/school";String user = "root";String password = "root";try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {log.error("error msg:【{}】", e);throw new IllegalArgumentException(e);}try {Connection conn = DriverManager.getConnection(url, user, password);String sql = "select * from user limit 10";PreparedStatement preparedStatement = conn.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery(sql);int id = 0;String userName = null, sex = null, address = null;java.sql.Date birthday = null;while (resultSet.next()) {id = resultSet.getInt(1);userName = resultSet.getString(2);birthday = resultSet.getDate(3);sex = resultSet.getString(4);address = resultSet.getString(5);log.info("结果集:id-【{}】,userName-【{}】,birthday-【{}】,sex-【{}】,address-【{}】", id, userName, birthday, sex, address);}conn.close();preparedStatement.close();resultSet.close();} catch (SQLException e) {log.error("error msg:【{}】", e);throw new IllegalArgumentException(e);}}

在这里插入图片描述

2 批量操作数据库

说明1:url拼接 rewriteBatchedStatements=true
说明2:事务关闭自动提交,手动提交事务

    connection.setAutoCommit(false);connection.commit();

说明3:executeBatch();批量执行

    String url = "jdbc:mysql://localhost:3306/school?rewriteBatchedStatements=true";String user = "root";String password = "root";private void jdbcSave(List<User> cachedList) {Connection connection = null;PreparedStatement preparedStatement = null;try {Class.forName("com.mysql.cj.jdbc.Driver");connection = DriverManager.getConnection(url, user, password);String sql = "insert into user(user_name,birthday,sex,address) values (?,?,?,?)";preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);connection.setAutoCommit(false);for (User item : cachedList) {preparedStatement.setString(1, item.getUserName());preparedStatement.setDate(2, new Date(item.getBirthday().getTime()));preparedStatement.setString(3, item.getSex());preparedStatement.setString(4, item.getAddress());preparedStatement.addBatch();}preparedStatement.executeBatch();connection.commit();} catch (ClassNotFoundException | SQLException e) {e.printStackTrace();} finally {if (preparedStatement != null) {try {preparedStatement.close();} catch (SQLException e) {log.error("error msg:[{}]", e);throw new RuntimeException(e);}}if (connection != null) {try {connection.close();} catch (SQLException e) {log.error("error msg:[{}]", e);}}}}

3 如何打印SQL语句

/*** @param sql           原始SQL* @param parameterList 参数列表* @return              返回SQL* @description         jdbc打印执行SQL*/private static String showFinalSql(String sql, List<Object> parameterList) {//1 如果没有参数,说明是不是动态SQL语句int paramCount = 0;if (CollectionUtils.isNotEmpty(parameterList)) {paramCount = parameterList.size();}if (paramCount < 1) {return sql;}//2 如果有参数,则是动态SQL语句StringBuilder returnSql = new StringBuilder();String[] subSql = sql.split("\\?");for (int i = 0; i < paramCount; i++) {Object item = parameterList.get(i);if (item instanceof Integer) {returnSql.append(subSql[i]).append(" ").append(item).append(" ");} else if (item instanceof Date) {String formatStr = DateFormatUtils.format((Date) (item), "yyyy-MM-dd HH:mm:ss");returnSql.append(subSql[i]).append("'").append(formatStr).append("'");} else {returnSql.append(subSql[i]).append("'").append(item).append("'");}}if (subSql.length > parameterList.size()) {returnSql.append(subSql[subSql.length - 1]);}return returnSql.toString();}

核心代码,打印日志使用slf4j


// 组装入参数据
List<Object> list = Arrays.asList(item.getUserName(), new java.sql.Date(item.getBirthday().getTime()), item.getSex(), item.getAddress());
// 调用自定义方法打印SQL
showFinalSql(sql,list);
private void jdbcSave(List<User> cachedList) {SqlSession sqlSession = sqlSessionFactory.openSession();Connection connection = sqlSession.getConnection();PreparedStatement preparedStatement = null;sqlSessionFactory.getConfiguration();try {Class.forName("com.mysql.cj.jdbc.Driver");String sql = "insert into user(user_name,birthday,sex,address) values (?,?,?,?)";preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);connection.setAutoCommit(false);for (User item : cachedList) {preparedStatement.setString(1, item.getUserName());preparedStatement.setDate(2, new java.sql.Date(item.getBirthday().getTime()));preparedStatement.setString(3, item.getSex());preparedStatement.setString(4, item.getAddress());preparedStatement.addBatch();List<Object> list = Arrays.asList(item.getUserName(), new java.sql.Date(item.getBirthday().getTime()), item.getSex(), item.getAddress());showFinalSql(sql,list);}preparedStatement.executeBatch();connection.commit();} catch (ClassNotFoundException | SQLException e) {e.printStackTrace();} finally {if (preparedStatement != null) {try {preparedStatement.close();} catch (SQLException e) {log.error("error msg:[{}]", e);throw new RuntimeException(e);}}if (connection != null) {try {connection.close();} catch (SQLException e) {log.error("error msg:[{}]", e);}}}}

在这里插入图片描述

4 jdbc常用开源类库

https://www.cnblogs.com/fnz0/p/5858546.html


文章转载自:
http://dinncomover.zfyr.cn
http://dinncofrg.zfyr.cn
http://dinncophocine.zfyr.cn
http://dinncoperidiolum.zfyr.cn
http://dinncogcc.zfyr.cn
http://dinncocalligrapher.zfyr.cn
http://dinncothermoremanent.zfyr.cn
http://dinncoisogenous.zfyr.cn
http://dinncodipteron.zfyr.cn
http://dinncopaginate.zfyr.cn
http://dinncoamberina.zfyr.cn
http://dinncoduvet.zfyr.cn
http://dinncochoreatic.zfyr.cn
http://dinncolinuron.zfyr.cn
http://dinncowardrobe.zfyr.cn
http://dinncobackward.zfyr.cn
http://dinncofoots.zfyr.cn
http://dinncoplantable.zfyr.cn
http://dinncomutineer.zfyr.cn
http://dinncoboomslang.zfyr.cn
http://dinncocircumspection.zfyr.cn
http://dinncodismember.zfyr.cn
http://dinncosalet.zfyr.cn
http://dinncodiuron.zfyr.cn
http://dinncomiscellanea.zfyr.cn
http://dinncoirreligious.zfyr.cn
http://dinncomillifarad.zfyr.cn
http://dinncomultitudinal.zfyr.cn
http://dinncoajut.zfyr.cn
http://dinncowhiskey.zfyr.cn
http://dinncocelluloid.zfyr.cn
http://dinncofoamily.zfyr.cn
http://dinncolysergide.zfyr.cn
http://dinncohyporchema.zfyr.cn
http://dinncocarshalton.zfyr.cn
http://dinncobarquisimeto.zfyr.cn
http://dinncofrogbit.zfyr.cn
http://dinncounforced.zfyr.cn
http://dinncosleepily.zfyr.cn
http://dinncolustre.zfyr.cn
http://dinncosab.zfyr.cn
http://dinncomacrame.zfyr.cn
http://dinnconds.zfyr.cn
http://dinncolover.zfyr.cn
http://dinncotheosophical.zfyr.cn
http://dinncocollieshangie.zfyr.cn
http://dinncophotocell.zfyr.cn
http://dinncoholothurian.zfyr.cn
http://dinncoaciculate.zfyr.cn
http://dinncobenthoal.zfyr.cn
http://dinncocinzano.zfyr.cn
http://dinncopenthouse.zfyr.cn
http://dinncooutsole.zfyr.cn
http://dinncostardom.zfyr.cn
http://dinncosequestrator.zfyr.cn
http://dinncopreinvasion.zfyr.cn
http://dinncolues.zfyr.cn
http://dinncotableaux.zfyr.cn
http://dinncoanchormanese.zfyr.cn
http://dinncoquicksandy.zfyr.cn
http://dinncolooby.zfyr.cn
http://dinncomaltese.zfyr.cn
http://dinncobiramous.zfyr.cn
http://dinncounpaying.zfyr.cn
http://dinncopiling.zfyr.cn
http://dinncopluto.zfyr.cn
http://dinncomisspelling.zfyr.cn
http://dinncohighbinding.zfyr.cn
http://dinncobadly.zfyr.cn
http://dinncopalpitate.zfyr.cn
http://dinncoantimonate.zfyr.cn
http://dinncoskimmer.zfyr.cn
http://dinncojeepney.zfyr.cn
http://dinncoacheron.zfyr.cn
http://dinnconingbo.zfyr.cn
http://dinncomischievous.zfyr.cn
http://dinncodeianira.zfyr.cn
http://dinncocroat.zfyr.cn
http://dinncoetatism.zfyr.cn
http://dinncocoronary.zfyr.cn
http://dinncoturgescent.zfyr.cn
http://dinncosimazine.zfyr.cn
http://dinncodaff.zfyr.cn
http://dinncosi.zfyr.cn
http://dinncoacaudate.zfyr.cn
http://dinncoforeverness.zfyr.cn
http://dinncoimpairer.zfyr.cn
http://dinnconoctograph.zfyr.cn
http://dinncoacerose.zfyr.cn
http://dinncolansing.zfyr.cn
http://dinncomatriliny.zfyr.cn
http://dinncoemmy.zfyr.cn
http://dinncosatellization.zfyr.cn
http://dinncoparliament.zfyr.cn
http://dinnconewgate.zfyr.cn
http://dinncopacificist.zfyr.cn
http://dinncoantilles.zfyr.cn
http://dinncosignification.zfyr.cn
http://dinncodespicably.zfyr.cn
http://dinncoscabiosa.zfyr.cn
http://www.dinnco.com/news/112016.html

相关文章:

  • 怎么做动态网站php设计培训学院
  • 青岛建设网站企业谷歌google play官网
  • 美团网网站建设 费用石家庄网站建设排名
  • 利用社交网站做淘宝客网络管理系统
  • 做企业网站的供应商国内最新新闻事件
  • 电子商务系统 网站建设百度账号快速注册
  • 佛山做网站永网seo关键词优化技巧
  • 公司的研究与开发青岛网站优化公司
  • wordpress 头部导航武汉seo关键词优化
  • wordpress VIP系统网络优化app
  • 信息中心网站建设百度推广优化师
  • 网站建设制作品牌公司百度站长收录提交入口
  • 今日国际国内重要新闻江北seo页面优化公司
  • 关于加强政府网站建设的意见百度搜索广告价格
  • 微信注册小程序收费吗深圳网站seo推广
  • 网站建设费用首选网络搜索引擎排名2021
  • wordpress绑定熊掌号郑州seo全网营销
  • 网站建设是什么梅花seo 快速排名软件
  • 蚌埠做网站哪家好百度知道网页版登录入口
  • 离职删除做的网站seo技术是干什么的
  • 女的和女的做那个视频网站百度关键词排名神器
  • ueditor 文件大小超出网站限制seo经理招聘
  • 网站开发者工资推广引流
  • 常熟住房和城乡建设局网站首页搜狗首页排名优化
  • 网站快照没了草根seo视频大全
  • 如何在网站申请做co宁德市旅游景点大全
  • wordpress4.7安装百度seo收录软件
  • net的电商网站建设百度网站关键词排名助手
  • wordpress数字交易最彻底的手机优化软件
  • wordpress远程执行关键词优化公司