statement-相关概述 Statement 对象用于将 SQL 语句发送到数据库中。实际上有三种 Statement 对象,它们都作为在给定连接上执行 SQL语句的包容器:Statement、PreparedStatement(它从 Statement 继承而来)和CallableStatement(它从 PreparedStatement 继承而来)。它们都专用于发送特定类型的 SQL 语句:Statement 对象用于执行不带参数的简单 SQL 语句;PreparedStatement 对象用于执行带或不带 IN参数的预编译 SQL 语句;CallableStatement 对象用于执行对数据库已存储过程的调用。 Statement 接口提供了执行语句和获取结果的基本方法。PreparedStatement 接口添加了处理 IN 参数的方法;而CallableStatement 添加了处理 OUT 参数的方法。 有些 DBMS将已存储过程中的每条语句视为独立的语句;而另外一些则将整个过程视为一个复合语句。在启用自动提交时,这种差别就变得非常重要,因为它影响什么时候调用commit 方法。在前一种情况中,每条语句单独提交;在后一种情况中,所有语句同时提交。 1、创建 Statement 对象 建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句。Statement 对象用 Connection 的方法createStatement 创建,如下列代码段中所示:
1 2 |
Connection con = DriverManager.getConnection(url, "sunny",""); Statement stmt = con.createStatement(); |
为了执行 Statement 对象,被发送到数据库的 SQL 语句将被作为参数提供给 Statement 的方法:
1 |
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROMTable2"); |
2、使用 Statement 对象执行语句 Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和execute。使用哪一个方法由 SQL 语句所产生的内容决定。 方法 executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。 方法 executeUpdate 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQLDDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。 方法 execute用于执行返回多个结果集、多个更新计数或二者组合的语句。因为多数程序员不会需要该高级功能,所以本概述后面将在单独一节中对其进行介绍。 执行语句的所有方法都将关闭所调用的 Statement 对象的当前打开结果集(如果存在)。这意味着在重新执行 Statement对象之前,需要完成对当前 ResultSet 对象的处理。 应注意,继承了 […]
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
try { boolean auto = conn.getAutoCommit(); conn.setAutoCommit(false); Statement stat = conn.createStatement(); Iterator<String> it = sourcetime.iterator(); // System.out.println("get sourcetime iterator successfully"); while (it.hasNext()) { String st = it.next(); //System.out.println("this st*_*"+st+"*_*"); Pattern subpat = Pattern.compile("[|]"); // 创建Pattern实例 String mac_time[] = subpat.split(st); int time = Integer.parseInt(mac_time[1]); // time String mac = mac_time[0]; // System.out.println("this::::"+mac+","+gname +","+time); String sql = "insert into edges (source,target,time) values('" + mac + "','" + gname + "','" + time + "')"; stat.addBatch(sql); } stat.executeBatch(); conn.commit(); logger.info("insert "+gname+" data into DB edges finished"); conn.setAutoCommit(auto); } catch (SQLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } |
from:https://www.cnblogs.com/ivywenyuan/p/4148541.html
View Details1. 在工作中很多时候需要执行一个SQL脚本文件到数据库中作为初始化数据;spring提供了一个工具类ScriptUtils,具体用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); } } |
2. 但是有时候我们的SQL脚本文件很大,甚至是几百mb,这样容易造成内存溢出的情况,因此我写了一个工具类,对SQL脚本进行拆解,然后批量执行。 这样每批量执行后,就清空缓存中的SQL,因此解决内存溢出问题。如下: 具体还没有用大数据量的脚本测试,等周一到公司再测试一下吧,哈哈哈。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
@SpringBootTest class ExecuteSqlScriptApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException, IOException { Resource classPathResource = new ClassPathResource("init/student.sql"); ScriptUtils.executeSqlScript(dataSource.getConnection(), classPathResource); // 分批处理SQL脚本 batchExecuteSql(classPathResource, 5); } /** * SQL脚本分解执行 * @param resource SQL脚本资源 * @param batchNumber 每多少条SQL执行一次 * @throws SQLException * @throws IOException */ public void batchExecuteSql(Resource resource, int batchNumber) throws SQLException, IOException { Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement(); BufferedReader bufferedReader = null; try { //获取字符缓冲流 bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream())); int l; int i = 0; StringBuilder sql = new StringBuilder(); while ((l = bufferedReader.read()) != -1) { char read = (char) l; sql.append(read); if (read == ';') { // 一个完整的SQL语句 i ++; statement.addBatch(sql.toString()); if (i % batchNumber == 0) { System.out.println("每" + batchNumber + "条语句批量执行开始......"); statement.executeBatch(); statement.clearBatch(); System.out.println("每" + batchNumber + "条语句批量执行结束......"); } //清除StringBuilder中的SQL语句 sql.delete(0, sql.length()); } } if (i % batchNumber != 0) { System.out.println("执行最后不足" + batchNumber + "条语句开始!!!"); statement.executeBatch(); statement.clearBatch(); System.out.println("执行最后不足" + batchNumber + "条语句结束!!!"); } } finally { if (bufferedReader != null) { bufferedReader.close(); } if (connection != null) { connection.close(); } if (statement != null) { statement.close(); } } } } |
from:https://www.cnblogs.com/fangyan1994/p/14123592.html
View Details服务器:centos7 apollo:1.7 开发环境:springboot2.3 1.整合
1 2 3 4 5 6 7 8 9 10 |
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.10</version> </dependency> |
1 2 3 4 |
app.id=dev apollo.meta=http://192.167.2.123:8080 #eureka地址 apollo.bootstrap.enable=true apollo.bootstrap.namespaces=application |
1 2 3 4 |
@SpringBootApplication @EnableApolloConfig public class ApolloServerApplication { } |
2.启动时异常
1 2 3 4 5 6 7 8 |
Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:30.617 WARN 42252 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:31.623 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:33.624 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] 2021-02-01 19:02:33.628 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 1 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:34.625 WARN 42252 --- [ main] c.c.f.a.i.RemoteConfigRepository : Load config failed, will retry in 1 SECONDS. appId: dev, cluster: default, namespaces: application 2021-02-01 19:02:35.629 WARN 42252 --- [ngPollService-1] c.c.f.a.i.RemoteConfigLongPollService : Long polling failed, will retry in 2 seconds. appId: dev, cluster: default, namespaces: application, long polling url: http://192.168.1.26:8080/notifications/v2?cluster=default&appId=dev&ip=192.168.1.2¬ifications=%5B%7B%22namespaceName%22%3A%22application%22%2C%22notificationId%22%3A-1%7D%5D, reason: Could not complete get operation [Cause: connect timed out] 2021-02-01 19:02:36.625 WARN 42252 --- [ main] c.c.f.a.i.LocalFileConfigRepository : Sync config from upstream repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository failed, reason: Load Apollo Config failed - appId: dev, cluster: default, namespace: application, url: http://192.168.1.26:8080/configs/dev/default/application?ip=192.168.1.2 [Cause: Could not complete get operation [Cause: connect timed out]] |
3.分析原因 http://192.168.1.26:8080 这个地址不是我eureka的地址,所以才找的时候会找不到 4.解决办法 启动的环境变量中加入: -Dapollo.configService=http://192.167.2.123:8080 将地址改为eureka的地址就好 ———————————————— 版权声明:本文为CSDN博主「weixin_38777065」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_38777065/article/details/113527235
View Details