JDBC 查询语句参数
2018-03-21 09:33
162 查看
如果在一个表中有很多的字段,而我们需要查询这张表中的太多数字段。这样,我们就需要写出很多的占位符。那么可以使用循环语句。public class DBHelper {
// 创建连接对象
Connection conn = null;
// 创建PreparedStatement对象
PreparedStatement pstmt = null;
// 创建结果集ResultSet
ResultSet rs = null;
// 数据库驱动
public final static String DRIVER = "com.mysql.jdbc.Driver";
// url
public final static String URL = "jdbc:mysql://localhost:3306/demo_test";
// 数据库用户名
public final static String DBNAME = "root";
// 数据库密码
public final static String DBPASS = "root";
/**
* 获取数据库连接
*
* @return
* @throws ClassNotFoundException
* @throws SQLException
* Connection
*/
public Connection getConn() throws ClassNotFoundException, SQLException {
try {
// 注册驱动
Class.forName(DRIVER);
// 获得数据库连接
conn = DriverManager.getConnection(URL, DBNAME, DBPASS);
} catch (Exception e) {
e.printStackTrace();
}
// 返回连接
return conn;
}
/**
* 释放资源
*
* @param conn
* 数据库连接
* @param pstmt
* PreparedStatement对象
* @param rs
* 结果集
*/
public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
/* 如果rs不空,关闭rs */
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 如果pstmt不空,关闭pstmt */
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 如果conn不空,关闭conn */
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 查询
*
* @param sql
* 预编译的 SQL 语句
* @param param
* 预编译的 SQL 语句中的‘?’参数的字符串数组
* @return 影响的条数
*/
public ResultSet executeQuerySQL(String preparedSql, Object[] param) {
/* 处理SQL,执行SQL */
try {
// 得到数据库连接
conn = getConn();
// 得到PreparedStatement对象
pstmt = conn.prepareStatement(preparedSql);//执行sql语句
if (param != null) {
for (int i = 0; i < param.length; i++) {
// 为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
// 执行SQL语句,获取结果集ResultSet
rs = pstmt.executeQuery();
} catch (ClassNotFoundException e) {
// 处理ClassNotFoundException异常
e.printStackTrace();
} catch (SQLException e) {
// 处理SQLException异常
e.printStackTrace();
}
return rs;
}
}
// 创建连接对象
Connection conn = null;
// 创建PreparedStatement对象
PreparedStatement pstmt = null;
// 创建结果集ResultSet
ResultSet rs = null;
// 数据库驱动
public final static String DRIVER = "com.mysql.jdbc.Driver";
// url
public final static String URL = "jdbc:mysql://localhost:3306/demo_test";
// 数据库用户名
public final static String DBNAME = "root";
// 数据库密码
public final static String DBPASS = "root";
/**
* 获取数据库连接
*
* @return
* @throws ClassNotFoundException
* @throws SQLException
* Connection
*/
public Connection getConn() throws ClassNotFoundException, SQLException {
try {
// 注册驱动
Class.forName(DRIVER);
// 获得数据库连接
conn = DriverManager.getConnection(URL, DBNAME, DBPASS);
} catch (Exception e) {
e.printStackTrace();
}
// 返回连接
return conn;
}
/**
* 释放资源
*
* @param conn
* 数据库连接
* @param pstmt
* PreparedStatement对象
* @param rs
* 结果集
*/
public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
/* 如果rs不空,关闭rs */
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 如果pstmt不空,关闭pstmt */
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/* 如果conn不空,关闭conn */
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 查询
*
* @param sql
* 预编译的 SQL 语句
* @param param
* 预编译的 SQL 语句中的‘?’参数的字符串数组
* @return 影响的条数
*/
public ResultSet executeQuerySQL(String preparedSql, Object[] param) {
/* 处理SQL,执行SQL */
try {
// 得到数据库连接
conn = getConn();
// 得到PreparedStatement对象
pstmt = conn.prepareStatement(preparedSql);//执行sql语句
if (param != null) {
for (int i = 0; i < param.length; i++) {
// 为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
// 执行SQL语句,获取结果集ResultSet
rs = pstmt.executeQuery();
} catch (ClassNotFoundException e) {
// 处理ClassNotFoundException异常
e.printStackTrace();
} catch (SQLException e) {
// 处理SQLException异常
e.printStackTrace();
}
return rs;
}
}
相关文章推荐
- jdbc查询语句中in后传参数问题的解决方案
- sql语句里有中文字符,JDBC查询不出来结果
- JDBC-Driver、PostgreSQL的SQL语句参数上限错误分析及解决办法(源码)
- hibernate查询语句hql中的占位符?参数与命名参数:name设值方式搞混
- sparkSQL里 sql语句,dataframe,Thrift Server JDBC都可以实现对数据的查询,过滤等操作, 哪这3种情况分别是什么情况下使用
- jdbc中想同时使用两个sql查询语句,必须创建两套statement和resultSet
- 两种使用nuzt接收动态参数编写SQL语句进行分页查询
- 在sql语句中,怎样将参数做为表名传递到查询语句中
- JDBC获取SQL查询语句返回记录数的三种方式效率比较
- .NET中oledb访问access含子查询的语句的参数置换顺序
- 第17天(就业班) jdbc连接数据库、statement执行DDL语句、statement执行DQL查询语句、preparedstatement、callablestatement、通讯录改造jdb
- JDBC实现模糊查询、DML语句操作
- hql语句命名参数查询示例
- 使用HQL语句的按照参数名字查询数据库信息的时候 “=:”和参数之间不能存在空格,否则会报错
- 根据参数指定的列名和值查询员工信息(使用动态sql语句)
- 带查询参数 可分页 的 T-SQL 语句写法
- 前台页面查询参数多的话,建议将其封装成querybean传到DAO层,在DAO层构建SQL语句:例子
- JDBC 处理sql查询多个不确定参数
- 多条件查询--使用dapper命令参数动态拼接出最安全的sql语句
- 让JAVA的JDBC支持命名参数的SQL语句