本文禁止转载,仅用于个人学习!
MVC框架结构:
操作步骤:
1、下载一个JDBC驱动
2、将驱动复制到当前项目
3、挂载驱动:右键项目--->properties--->resource--->Java build path--->libraries--->add JAR resource
4、创建以下包:javaBean,jdbc,dao,service,control,view,test
5、在各个包下创建class,完成操作
DBC程序访问数据库的步骤:
1、加载并注册驱动程序
2、创建一个connection连接
3、创建一个statment对象
4、执行查询或更新
5、结果处理
6、关闭连接
自己写了一个封装工具类,以后方便使用jdbc
代码实现如下:
package com.oracle.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCutil {
private static Connection conn=null;
private PreparedStatement pst=null;
private String driver="oracle.jdbc.driver.OracleDriver";
private String url="jdbc:oracle:thin:@10.25.43.157:1521:orcl";
private String userName="scott";
private String pwd="tiger";
/*mySQL连接
private String driver="com.mysql.jdbc.Driver";
//private String url="jdbc:mysql://localhost:3306/test?useSSL=false";
private String url="jdbc:mysql://localhost:3306/test2?
useUnicode=true&characterEncoding=UTF-8&useSSL=false";
private String userName="root";
private String pwd="root";*/
public Connection getConnection(){
try {
Class.forName(driver);
System.out.println("加载驱动成功");
if(conn==null){
conn = DriverManager.getConnection(url,userName,pwd);
}
System.out.println("创建Connection连接成功");
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("创建Connection连接失败");
e.printStackTrace();
}
return conn;
}
public int executeUpdate(String sql) throws SQLException {
int count=0;
try {
pst=conn.prepareStatement(sql);
count=pst.executeUpdate();
System.out.println("执行更新操作成功,有"+count+"条记录更新");
} catch (SQLException e) {
System.out.println("执行更新操作失败");
e.printStackTrace();
throw new SQLException();
}
return count;
}
public int executeUpdate(String sql,Object[] obj)throws SQLException{
int count=0;
try {
pst=conn.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pst.setObject(i+1, obj[i]);
}
count=pst.executeUpdate();
System.out.println("执行更新操作成功,有"+count+"条记录更新");
} catch (SQLException e) {
System.out.println("执行更新操作失败");
e.printStackTrace();
throw new SQLException();
}
return count;
}
public ResultSet executeQuery(String sql)throws SQLException{
ResultSet rs=null;
try {
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
System.out.println("执行查询操作成功");
} catch (SQLException e) {
System.out.println("执行查询操作失败");
e.printStackTrace();
throw new SQLException();
}
return rs;
}
public ResultSet executeQuery(String sql,Object[] obj)throws SQLException{
ResultSet rs=null;
try {
pst=conn.prepareStatement(sql);
for(int i=0;i<obj.length;i++){
pst.setObject(i+1, obj[i]);
}
rs=pst.executeQuery();
System.out.println("执行查询操作成功");
} catch (SQLException e) {
System.out.println("执行查询操作失败");
e.printStackTrace();
throw new SQLException();
}
return rs;
}
public void beginTranaction(){
try {
conn.setAutoCommit(false);
System.out.println("开始事务");
} catch (SQLException e) {
System.out.println("开始事务出现异常");
e.printStackTrace();
}
}
public void commit(){
try {
conn.commit();
System.out.println("事务提交成功");
} catch (SQLException e) {
System.out.println("事务提交异常");
e.printStackTrace();
}
}
public void rollback(){
try {
conn.rollback();;
System.out.println("事务回滚成功");
} catch (SQLException e) {
System.out.println("事务回滚异常");
e.printStackTrace();
}
}
public void close(){
try {
if(pst!=null){
pst.close();
pst=null;
System.out.println("关闭PreparedStatement对象");
}
if(conn!=null){
conn.close();
conn=null;
System.out.println("关闭Connection对象");
}
} catch (SQLException e) {
System.out.println("关闭PreparedStatement,Connection对象异常");
e.printStackTrace();
}
}
public static void main(String[] args) {
JDBCutil db=new JDBCutil();
db.getConnection();
db.close();
System.out.println("over");
}
}