JDBC连接数据库以及简单的操作

package com.zhiyuan.jdbc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* <p>Title: 数据库基本操作
* <p>Company:
* @description 数据库的连接和关闭
* @author LIANG ZHIYUAN
* @date 2016年1月9日下午4:07:33
* @version 1.0
*/
public class dbUtil {
/**
* JDBC驱动类名称
*/
private static final String JDBC_DRIVER_NAME="com.mysql.jdbc.Driver";
/**
* DB地址
*/
private static final String DB_URL="jdbc:mysql://localhost:3306/db_book?characterEncoding=utf8";
/**
* DB用户
*/
private static final String DB_USER="root";
/**
* DB密码
*/
private static final String DB_PASSWORD="123456";
/**
* Connection
*/
private static Connection conn;

/**
* <p>DB连接
* @return Connection
*/
public static Connection getConnection(){
try {
// 加载DB驱动类,并注册到DriverMANAGER
Class.forName(JDBC_DRIVER_NAME);
System.out.println("数据库驱动加载成功!");
conn=DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
System.out.println("数据库连接成功!");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}

/**
* <p>DB连接关闭
*/
public static void closeConn(){
if (conn!=null) {
try {
conn.close();
System.out.println("数据库连接关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接关闭失败!");
}
}
}

/**
* <p>Statement关闭,DB连接关闭
* @param stmt Statement
* @param conn Connection
*/
public static void closeAll(Statement stmt,Connection conn){
if (stmt!=null) {
try {
stmt.close();
System.out.println("Statement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Statement关闭失败!");
}
}
if (conn!=null) {
try {
conn.close();
System.out.println("数据库连接关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接关闭失败!");
}
}
}


/**
* <p>PreparedStatement关闭,DB连接关闭
* @param pstmt PreparedStatement
* @param conn Connection
*/
public static void closeAll(PreparedStatement pstmt,Connection conn){
if (pstmt!=null) {
try {
pstmt.close();
System.out.println("PreparedStatement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("PreparedStatement关闭失败!");
}
}
if (conn!=null) {
try {
conn.close();
System.out.println("数据库连接关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接关闭失败!");
}
}
}

/**
* <p>ResultSet关闭,PreparedStatement关闭,DB连接关闭
* @param rs ResultSet
* @param pstmt PreparedStatement
* @param conn Connection
*/
public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
if (rs!=null) {
try {
rs.close();
System.out.println("ResultSet关闭成功!");
} catch (SQLException e) {
System.out.println("ResultSet关闭失败!");
e.printStackTrace();
}
}
if (pstmt!=null) {
try {
pstmt.close();
System.out.println("PreparedStatement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("PreparedStatement关闭失败!");
}
}
if (conn!=null) {
try {
conn.close();
System.out.println("数据库连接关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接关闭失败!");
}
}
}

/**
* <p>Statement关闭,DB连接关闭
* @param stmt Statement
*/
public static void closeAll(Statement stmt){
if (stmt!=null) {
try {
stmt.close();
System.out.println("Statement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Statement关闭失败!");
}
}
closeConn();
}

/**
* <p>PreparedStatement关闭,DB连接关闭
* @param pstmt PreparedStatement
*/
public static void closeAll(PreparedStatement pstmt){
if (pstmt!=null) {
try {
pstmt.close();
System.out.println("PreparedStatement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("PreparedStatement关闭失败!");
}
}
closeConn();
}

/**
* <p>ResultSet关闭,PreparedStatement关闭,DB连接关闭
* @param rs ResultSet
* @param pstmt PreparedStatement
*/
public static void closeAll(ResultSet rs,PreparedStatement pstmt){
if (pstmt!=null) {
try {
pstmt.close();
System.out.println("PreparedStatement关闭成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("PreparedStatement关闭失败!");
}
}
closeConn();
}
}

package com.zhiyuan.jdbc.demo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhiyuan.jdbc.model.PriceLevel;
import com.zhiyuan.jdbc.util.dbUtil;

/**
* <p>Title: DB演示DEMO
* <p>Company:
* @description DB操作演示
* @author LIANG ZHIYUAN
* @date 2016年1月10日上午12:15:05
* @version 1.4
*/
public class Demo5 {

/**
* <p>DB插入操作
* @param prl PriceLevel数据模型对象
* @return 操作的行数
* @throws SQLException
*/
public static int dbInsert(PriceLevel prl) throws SQLException{
// 获取数据库连接
Connection conn=dbUtil.getConnection();
// 数据库操作的SQL数据
String sql="insert into t_pricelevel values(?,?,?,?);";
// 结果
int result=0;
PreparedStatement pstmt=null;
// 获取PreparedStatement
pstmt=conn.prepareStatement(sql);
// 设置参数
pstmt.setInt(1, prl.getId());
pstmt.setInt(2, prl.getPriceLevel());
pstmt.setFloat(3, prl.getPrice());
pstmt.setString(4, prl.getDescription());
result=pstmt.executeUpdate();
// 全部关闭
dbUtil.closeAll(pstmt, conn);
return result;
}

/**
* <p>DB更新操作
* @param prl PriceLevel数据模型对象
* @return 操作的行数
* @throws SQLException
*/
public static int dbUpdate(PriceLevel prl) throws SQLException{
// 获取数据库连接
Connection conn=dbUtil.getConnection();
// 数据库操作的SQL数据
String sql="update t_pricelevel set priceLevel=?,price=?,description=? where id=?";
// 结果
int result=0;
PreparedStatement pstmt=null;
// 获取PreparedStatement
pstmt=conn.prepareStatement(sql);
// 设置参数
pstmt.setInt(1, prl.getPriceLevel());
pstmt.setFloat(2, prl.getPrice());
pstmt.setString(3, prl.getDescription());
pstmt.setInt(4, prl.getId());
result=pstmt.executeUpdate();
// 全部关闭
dbUtil.closeAll(pstmt, conn);
return result;
}

/**
* <p>DB删除操作
* @param id 图书价格等价ID
* @return 操作的行数
* @throws SQLException
*/
public static int dbDelete(int id) throws SQLException{
// 获取数据库连接
Connection conn=dbUtil.getConnection();
// 数据库操作的SQL数据
String sql="delete from t_pricelevel where id=?";
// 结果
int result=0;
PreparedStatement pstmt=null;
// 获取PreparedStatement
pstmt=conn.prepareStatement(sql);
// 设置参数
pstmt.setInt(1, id);
result=pstmt.executeUpdate();
// 全部关闭
dbUtil.closeAll(pstmt);
return result;
}

/**
* <p>DB查询操作
* @return PriceLevel元素的List
* @throws SQLException
*/
public static List<PriceLevel> dbSelect() throws SQLException{
List<PriceLevel> prlList=new ArrayList<>();
// 获取数据库连接
Connection conn=dbUtil.getConnection();
// 数据库操作的SQL数据
String sql="select * from t_pricelevel";
PreparedStatement pstmt=null;
// 获取PreparedStatement
pstmt=conn.prepareStatement(sql);
// 获取ResultSet
ResultSet rs=pstmt.executeQuery();
// 遍历结果集
while(rs.next()){
int id=rs.getInt("id");
int priceLevel=rs.getInt("priceLevel");
float price=rs.getFloat("price");
String description=rs.getString("description");
PriceLevel prl=new PriceLevel(id, priceLevel, price, description);
// 向列表添加元素
prlList.add(prl);
}
// 全部关闭
dbUtil.closeAll(rs,pstmt, conn);
return prlList;
}

/**
* <p>主方法(程序入口)
* @param args 命令行参数
* @throws SQLException
*/
public static void main(String[] args) throws SQLException{
List<PriceLevel> prlList=dbSelect();
for (PriceLevel priceLevel : prlList) {
System.out.println(priceLevel.toString());
}

}

}

原文地址:https://www.cnblogs.com/LEARN4J/p/5120064.html