逆袭之旅DAY31.XIA.JDBC

时间:2024-08-07 16:07:26

2018-07-31

MySQL

 package oop_emp.com.neusoft.dao;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; /**
* 数据库操作基础类
*
* @author xxf
*
*/
public class BaseDao {
// 创建4个常量(数据库连接地址,数据库驱动类地址,数据库登录权限名,登录密码)
private static final String DRIVER="com.mysql.jdbc.Driver";
private static final String URL="jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8";
private static final String UNAME="root";
private static final String UPWD="123456"; // 使用静态块加载数据库的驱动
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 声明三个核心接口对象(Connection,PreparedStatment,ResultSet)
private Connection conn = null;// 数据库连接的对象
private PreparedStatement pstmt = null;// SQL命令预处理并执行操作的对象
protected ResultSet res = null;// 查询后返回的结果集对象
// 编写创建数据库连接对象的方法(DriverManager)
private void getConn() {
try {
conn = DriverManager.getConnection(URL, UNAME, UPWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 编写关闭数据库释放资源的方法
protected void colseAll() {
if (null != res) {
try {
res.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} // 编写数据库的查询方法
protected ResultSet excuteSelect(String sql, Object[] params) {
// 调用数据库连接对象的方法
this.getConn();
// 创建预处理对象
try {
pstmt = conn.prepareStatement(sql);
// 通过for循环对参数进行预处理
if (null != params) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
}
// 操作查询并返回结果集
res = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
// 编写数据库的增删改的方法
protected int excuteEdit(String sql,Object[] params){
int count = 0;
//调用数据库连接对象的方法
this.getConn();
try {
//创建SQL命令预处理执行操作的对象
pstmt = conn.prepareStatement(sql);
//对参数进行预处理
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
//接收操作执行返回的行数
count = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.colseAll();
}
return count;
} }
 public class TestJDBC3
{
public static void main(String[] args)
{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/otherww? useUnicode=true&characterEncoding=utf8";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null; String sql = "select name,money,id,age from student"; try
{
// 1,加载驱动
Class.forName(driver);
// 2,获得连接
conn = DriverManager.getConnection(url, user, password);
// 3,获得状态集
pstmt = conn.prepareStatement(sql);
// 4,获得结果集
rs = pstmt.executeQuery();
// 5,处理结果集
while (rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
double money = rs.getDouble("money");
Student s = new Student();
s.setId(id);
s.setStuName(name);
s.setAge(age);
s.setMoney(money);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
//6,释放资源
try
{
if(null != rs)
{
rs.close();
}
if(null != pstmt)
{
pstmt.close();
}
if(null != conn)
{
conn.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}

Oracle

 package com.neusoft.dao;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; /**
* 数据库操作基础类
*
* @author xxf
*
*/
public class BaseDao {
// 创建4个常量(数据库连接地址,数据库驱动类地址,数据库登录权限名,登录密码)
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static final String UNAME = "scott";
private static final String UPWD = "123"; // 使用静态块加载数据库的驱动
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 声明三个核心接口对象(Connection,PreparedStatment,ResultSet)
private Connection conn = null;// 数据库连接的对象
private PreparedStatement pstmt = null;// SQL命令预处理并执行操作的对象
protected ResultSet res = null;// 查询后返回的结果集对象
// 编写创建数据库连接对象的方法(DriverManager)
private void getConn() {
try {
conn = DriverManager.getConnection(URL, UNAME, UPWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 编写关闭数据库释放资源的方法
protected void colseAll() {
if (null != res) {
try {
res.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} // 编写数据库的查询方法
protected ResultSet excuteSelect(String sql, Object[] params) {
// 调用数据库连接对象的方法
this.getConn();
// 创建预处理对象
try {
pstmt = conn.prepareStatement(sql);
// 通过for循环对参数进行预处理
if (null != params) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
}
// 操作查询并返回结果集
res = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
// 编写数据库的增删改的方法
protected int excuteEdit(String sql,Object[] params){
int count = 0;
//调用数据库连接对象的方法
this.getConn();
try {
//创建SQL命令预处理执行操作的对象
pstmt = conn.prepareStatement(sql);
//对参数进行预处理
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
//接收操作执行返回的行数
count = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.colseAll();
}
return count;
} }