jdbc调用存储过程
来源:互联网 发布:python爬虫视频教程 编辑:程序博客网 时间:2024/06/03 22:54
今天工作的时候经理写了个存储过程给我让我优化折线图表,由于从来没用过存储过程,苦恼了一会。为了方便以后使用,现记录一下。
一:连接信息
package com.cj.monitoringplatform.service;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import java.util.Properties;import org.apache.log4j.Logger;import com.cj.monitoringplatform.model.CarLog;import com.jfinal.kit.PathKit;public class CallStoredProcedures{Logger logger = Logger.getLogger(CallStoredProcedures.class); private static String jdbcConfig = PathKit.getRootClassPath() + File.separator + "db.properties"; protected Connection connection; protected Statement statement; protected PreparedStatement preparedStatement; protected ResultSet resultSet; //连接信息 public Connection getCon() throws ClassNotFoundException, SQLException { try { Properties properties = new Properties(); InputStream inputStream = new FileInputStream(jdbcConfig); properties.load(inputStream); String driverClassName = properties.getProperty("mysql.driverClass"); String url = properties.getProperty("mysql.jdbcUrl"); String username = properties.getProperty("mysql.userName"); String password = properties.getProperty("mysql.passWord"); Class.forName(driverClassName); connection = DriverManager.getConnection(url, username, password); } catch (IOException e) { throw new RuntimeException("初始化失败,配置文件 = " + jdbcConfig, e); } catch (Exception e) { throw new RuntimeException(e); } return connection; }
public List<CarLog> importFansFocusRecord(String devid,String startTime,String endTime,String type,Integer index) { List<CarLog> list=new ArrayList<CarLog>(); try { getCon(); CallableStatement callableStatement = connection.prepareCall("{call tpline(?,?,?,?,?)}");//问号代表存储过程的参数 callableStatement.setString(1,devid); callableStatement.setString(2,startTime); callableStatement.setString(3,endTime); callableStatement.setString(4, type); callableStatement.setInt(5,index); callableStatement.execute(); ResultSet resultSet = callableStatement.getResultSet(); System.out.println("开始输出存储过程所查询的所有数据"); while (resultSet.next()) { //System.out.println("time:"+resultSet.getString("time")+"\t pressure1:"+resultSet.getInt("pressure1")/1000+"\t pressure2:"+resultSet.getInt("pressure2")/1000+"\t pressure3:"+resultSet.getInt("pressure3")/1000); CarLog cLog=new CarLog(); cLog.set("time", resultSet.getDate("time")); cLog.set("pressure1", resultSet.getInt("pressure1")/1000); cLog.set("pressure2", resultSet.getInt("pressure2")/1000); cLog.set("pressure3", resultSet.getInt("pressure3")/1000); list.add(cLog); } closeAll(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return list; }
三:释放资源
// 释放资源 public void closeAll() { try { if (resultSet != null) { resultSet.close(); } if (statement != null) { statement.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null && !connection.isClosed()) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
阅读全文
1 0
- jdbc调用存储过程
- Jdbc调用存储过程
- jdbc调用存储过程
- jdbc调用存储过程
- JDBC调用存储过程
- JDBC调用存储过程
- JDBC调用存储过程
- Jdbc存储过程调用
- jdbc调用存储过程
- JDBC调用存储过程
- Jdbc调用存储过程
- JDBC调用存储过程
- JDBC调用存储过程
- jdbc调用存储过程
- JDBC调用存储过程
- jdbc调用存储过程
- jdbc调用存储过程
- jdbc调用存储过程
- 转载C++如何进阶学习
- Java使用Jacob操作word
- 使用vue-cli搭建vue项目完整版
- webpack Vue热加载配置好的确不行解决方法,限于webstorm编辑
- csdn如何转载别人的文章
- jdbc调用存储过程
- 洛谷P1312 Mayan游戏 DFS+剪枝(3s搜爆!)
- Android 屏幕适配
- JS高程知识点总结-5(引用类型)
- JSTL 将时间戳格式化
- 在 Android 8.0 中绕过 hwbinder 实现跨模块对 audio HAL 调用
- GitHub 发布年度开发者报告 JavaScript 依然最受欢迎
- 去除List集合中的重复值
- 模糊查询和聚会函数