java jdbc操作数据库通用代码

时间:2023-03-09 17:34:17
java jdbc操作数据库通用代码

1.准备工作

1》

java jdbc操作数据库通用代码

新建一个配置文件,名为jdbc.properties将其放入src中

2》在项目中导入jdbc驱动,注意连接不同的数据库,所用到的驱动是不一样的,这些在网上都能找到

具体导入jar的方法,请参照http://blog.****.net/mazhaojuan/article/details/21403717

2、代码

 import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; public class Main {
public static void main(String[] args) {
DBUtil dbUtil = new DBUtil();
dbUtil.R("select * from table");
}
} class DBUtil{
/**
* 得到数据库连接
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception{
//1.创建配置文件并得到对象输入流
InputStream is = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
//2.创建propetities
Properties jdbc = new Properties();
jdbc.load(is);
//3. 通过key-value 的方式得到对应的值
String driver = jdbc.getProperty("driver");
String url = jdbc.getProperty("url");
String user = jdbc.getProperty("user");
String password = jdbc.getProperty("password");
//4.加载运行时类对象
Class.forName(driver);
//5通过DriverManager得到连接
Connection connection = DriverManager.getConnection(url,user,password);
return connection; }
/**
* 释放资源的方法
* @param connection
* @param statement
* @param resultSet
*/
public void release(Connection connection,Statement statement,ResultSet resultSet){
try {
if(resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(statement!=null){
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
} }
/**
* 查询数据库的方法
* @param sql 字符串,要执行的sql语句 如果其中有变量的话,就用 ‘"+变量+"’
*/
public void R(String sql){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
while(resultSet.next()!=false){
//这里可以执行一些其他的操作
System.out.println(resultSet.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
release(connection, statement, resultSet);
}
}
/**
* 数据库记录增删改的方法
* @param sql 字符串,要执行的sql语句 如果其中有变量的话,就用 ‘"+变量+"’
*/
public void CUD(String sql){
Connection connection = null;
Statement statement = null;
int result = 0;
try {
connection = getConnection();
statement = connection.createStatement();
result = statement.executeUpdate(sql); //这里可以根据返回结果(影响记录的条数) 进行判断,该语句是否执行成功
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(connection, statement, null);
}
} }

3.预处理,其中上面的连接数据库及释放资源的方法不动

代码如下:

 import java.io.InputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; public class Main {
public static void main(String[] args) { }
} class DBUtil{
/**
* 得到数据库连接
* @return
* @throws Exception
*/
public Connection getConnection() throws Exception{
//1.创建配置文件并得到对象输入流
InputStream is = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties.txt");
//2.创建propetities
Properties jdbc = new Properties();
jdbc.load(is);
//3. 通过key-value 的方式得到对应的值
String driver = jdbc.getProperty("driver");
String url = jdbc.getProperty("url");
String user = jdbc.getProperty("user");
String password = jdbc.getProperty("password");
//4.加载运行时类对象
Class.forName(driver);
//5通过DriverManager得到连接
Connection connection = DriverManager.getConnection(url,user,password);
return connection; }
/**
* 释放资源的方法
* @param connection
* @param statement
* @param resultSet
*/
public void release(Connection connection,Statement statement,ResultSet resultSet){
try {
if(resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(statement!=null){
statement.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try{
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
} }
/**
* 查询数据库的方法
* @param sql 字符串,要执行的sql语句 如果其中有变量的话,就用 ‘"+变量+"’
*/
public void R(String sql, Object ...args){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i+1, args[i]);
}
resultSet = preparedStatement.executeQuery();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
while(resultSet.next()!=false){
//这里可以执行一些其他的操作
for (int i = 1; i <= columnCount; i++) {
System.out.println(resultSet.getString(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
release(connection, preparedStatement, resultSet);
}
}
/**
* 数据库记录增删改的方法
* @param sql 字符串,要执行的sql语句 如果其中有变量的话,就用 ‘"+变量+"’
*/
public void CUD(String sql, Object ...args){
Connection connection = null;
PreparedStatement preparedStatement = null;
int result = 0;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i+1, args[i]);
}
result = preparedStatement.executeUpdate();
//这里可以根据返回结果(影响记录的条数)进行判断,该语句是否执行成功
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
release(connection, preparedStatement, null);
}
} }

在预处理代码第87行使用了元数据获取集合中的列的数量  有关数据库 元数据,请参考文档上的相关接口:

DatabaseMetaData

ResultSetMetaData

注:本文为原创,如需转载请注明出处:http://www.cnblogs.com/zhuchenglin/p/7919803.html