JAVA使用Oracle数据库实现CRUD

JAVA使用Oracle数据库实现CRUD

一、数据库建表

-- Create table
create table EMP
(
  username VARCHAR2(10),
  password VARCHAR2(9),
  age      NUMBER(4),
  id       VARCHAR2(9)
)
tablespace USER_TS_DATA
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table EMP
  is '测试表';

二、Java连接Oracle数据库jar包

链接:https://pan.baidu.com/s/1OeyYbCnWo50ruVuQfi2Ksw
提取码:3nte
在这里插入图片描述
三、代码

1、封装连接数据库代码

package CRUD;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 * 数据库连接类
 */
public class DBUtil {

	public static Connection getConnection()
	{
		Connection connection = null;
		String 	USERNAME = ""; //数据库用户名
		String 	PASSWORD = "";  //密码
		String 	DRIVER = "oracle.jdbc.driver.OracleDriver"; 
		String 	URL = "jdbc:oracle:thin:@主机ip:1521:test"; //thin:小型驱动,驱动方式;test代表SERVICENAME(数据库的SID)
		try {
			 Class.forName(DRIVER);
			 connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
			 System.out.println("数据库连接成功!");
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
		return connection;
	}
	
	//关闭资源
	public static void close(Connection conn) {
		try {
			if(conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement pre) {
		try {
			if(pre != null) {
				pre.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet res) {
		try {
			if(res != null) {
				res.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2、Bean实体类代码

package CRUD;

//实体类
public class Bean {

	private String username;
	private String password;
	private int age;
	private String id;
	
	public Bean()
	{
		
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public Bean(String username, String password, int age,String id) {
		super();
		this.username = username;
		this.password = password;
		this.age = age;
		this.id = id;
	}
	public void update(String username, String password, int age,String id) {
        this.username = username;
        this.password = password;
        this.id = id;
        this.age = age;
    }
}

3、测试类

package CRUD;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
	
	//测试
	public static void main(String[] args) {
		Test test = new Test();
		//新增数据
		Bean bean = new Bean("用户名1","123456",23,"7");
		test.add(bean);
		test.load("7");
		
		//删除数据
//		test.delete("7");
//		test.load("7");		
	}
	
	//增加数据
	public void add(Bean bean) {
		Connection conn = DBUtil.getConnection();
		String sql = "insert into emp(username,password,age,id) values(?,?,?,?)";
		PreparedStatement pre = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setString(1, bean.getUsername());
			pre.setString(2, bean.getPassword());
			pre.setInt(3,bean.getAge());
			pre.setString(4, bean.getId());
			pre.executeUpdate(); //执行插入更新操作
			System.out.println("数据插入成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(pre);
			DBUtil.close(conn);
		}
	}
	//修改数据
	public void update(Bean bean) {
		Connection conn = DBUtil.getConnection();
		String sql = "update emp set username = ?,password = ?,age = ?,id = ?";
		PreparedStatement pre = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setString(1, bean.getUsername());
			pre.setString(2, bean.getPassword());
			pre.setInt(3,bean.getAge());
			pre.setString(4, bean.getId());
			pre.executeUpdate();
			System.out.println("修改数据成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(pre);
			DBUtil.close(conn);
		}
	}
	//删除数据(根据id删除)
	public void delete(String id) {
		Connection conn = DBUtil.getConnection();
		String sql = "delete from emp where id = ?";
		PreparedStatement pre = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setString(1, id);
			pre.executeQuery();
			System.out.println("数据删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(pre);
			DBUtil.close(conn);
		}
	}
	//查询数据(根据id查询)
	public Bean load(String id) {
		Connection conn = DBUtil.getConnection();
		String sql = "select * from emp where id = ?";
		PreparedStatement pre = null;
		ResultSet res = null;
		Bean bean = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setString(1, id);
			res = pre.executeQuery();
			while(res.next()) {
				System.out.println(res.getString("username") +","+ res.getString("password")+","+ res.getInt("age") +","+res.getString("id"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(pre);
			DBUtil.close(conn);
		}
		return bean;
	} 
}

你可能感兴趣的:(oracle数据库,Java,CRUD,java,数据库,jdbc,oracle)