起首与oracle创建连接
- package com.ma;
- import com.sun.media.sound.SoftTuning;
- import java.sql.*;
- public class JDBCtest {
- public static void main(String[] args) throws Exception {
- //1.加载驱动
- Class.forName("oracle.jdbc.driver.OracleDriver");
- //2.建立连接
- Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
- //3.测试建立连接
- System.out.println(connection);
- //4.定义sql语句
- String sql ="select * from emp";
- //5.准备静态处理块对象,将sql语句放置到静态代码块中
- Statement statement=connection.createStatement();
- //6.执行sql语句,返回值对象是结果集合
- ResultSet resultSet = statement.executeQuery(sql);
- //7.循环处理
- while (resultSet.next()){
- int anInt = resultSet.getInt(1);
- System.out.println(anInt);
- String ename = resultSet.getString("ename");
- System.out.println(ename);
- System.out.println("----------------");
- }
- //8.关闭连接
- statement.close();
- connection.close();
- }
- }
复制代码 测试连接
- package com.ma;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.util.function.Consumer;
- public class CreateTable {
- public static void main(String[] args) throws Exception {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
- Statement statement = connection.createStatement();
- String sql="create table psn(id number(10) primary key,name varchar2(10))";
- boolean execute = statement.execute(sql);
- System.out.println(execute);
- statement.close();
- connection.close();
- }
- }
复制代码 减少代码冗余性,创建代码块
- package com.ma.util;
- import java.sql.*;
- public class DBUtil {
- public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
- public static final String USENAME="scott";
- public static final String PASSWORE="123456";
- static {
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- public static Connection getConnection(){
- try {
- return DriverManager.getConnection(URL,USENAME,PASSWORE);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- public static void closeConnection(Connection connection, Statement statement) {
- if(statement!=null){
- try {
- statement.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
- public static void closeConnection(Connection connection, Statement statement, ResultSet resultSet) {
- if(resultSet!=null){
- try {
- resultSet.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if(statement!=null){
- try {
- statement.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
复制代码 下面创建增编削查
- package com.ma.dao;
- import com.ma.entity.Emp;
- public interface EmpDao {
- public void insert(Emp emp);
- public void delete(Emp emp);
- public void update(Emp emp);
- public Emp getEmpByEmpno(Integer empno);
- }
复制代码- package com.ma.dao.impl;
- import com.ma.dao.EmpDao;
- import com.ma.entity.Emp;
- import com.ma.util.DBUtil;
- import java.security.PublicKey;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class EmpDaolmpl implements EmpDao {
- @Override
- public void insert(Emp emp) {
- Connection connection=null;
- Statement statement=null;
- try {
- connection= DBUtil.getConnection();
- statement=connection.createStatement();
- String sql="insert into emp values("+emp.getEmpno()+",'"+emp.getEname()+"','"+emp.getJob()+"'," +
- ""+emp.getMrg()+",to_date('"+emp.getHiredate()+"','YYYY-MM-DD'),"+emp.getSal()+"," +
- ""+emp.getComm()+","+emp.getDeptno()+")";
- System.out.println(sql);
- int i = statement.executeUpdate(sql);
- System.out.println("受影响的行数是:"+i);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }finally {
- DBUtil.closeConnection(connection,statement);
- }
- }
- @Override
- public void delete(Emp emp) {
- Connection connection=null;
- Statement statement=null;
- try {
- connection= DBUtil.getConnection();
- statement=connection.createStatement();
- String sql="delete from emp where empno="+emp.getEmpno();
- System.out.println(sql);
- int i = statement.executeUpdate(sql);
- System.out.println("受影响的行数是:"+i);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }finally {
- DBUtil.closeConnection(connection,statement);
- }
- }
- @Override
- public void update(Emp emp) {
- Connection connection=null;
- Statement statement=null;
- try {
- connection= DBUtil.getConnection();
- statement=connection.createStatement();
- String sql="update emp set job='"+emp.getJob()+"'where empno ="+emp.getEmpno();
- System.out.println(sql);
- int i = statement.executeUpdate(sql);
- System.out.println("受影响的行数是:"+i);
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }finally {
- DBUtil.closeConnection(connection,statement);
- }
- }
- @Override
- public Emp getEmpByEmpno(Integer empno) {
- return null;
- }
- public static void main(String[] args) {
- EmpDao empDao=new EmpDaolmpl();
- Emp emp=new Emp(222,"sisi","SALES",1111,"2025-2-18",1500.0,500.0,10);
- // empDao.insert(emp);
- // empDao.delete(emp);
- empDao.update(emp);
- }
- }
复制代码- package com.ma.entity;
- import java.util.Date;
- public class Emp {
- private Integer empno;
- private String ename;
- private String job;
- private Integer mrg;
- private String hiredate;
- private Double sal;
- private Double comm;
- private Integer deptno;
- public Emp(){
- }
- public Emp(Integer empno, String ename, String job, Integer mrg, String hiredate, Double sal, Double comm, Integer deptno) {
- this.empno = empno;
- this.ename = ename;
- this.job = job;
- this.mrg = mrg;
- this.hiredate = hiredate;
- this.sal = sal;
- this.comm = comm;
- this.deptno = deptno;
- }
- public Integer getEmpno() {
- return empno;
- }
- public void setEmpno(Integer empno) {
- this.empno = empno;
- }
- public String getEname() {
- return ename;
- }
- public void setEname(String ename) {
- this.ename = ename;
- }
- public String getJob() {
- return job;
- }
- public void setJob(String job) {
- this.job = job;
- }
- public Integer getMrg() {
- return mrg;
- }
- public void setMrg(Integer mrg) {
- this.mrg = mrg;
- }
- public String getHiredate() {
- return hiredate;
- }
- public void setHiredate(String hiredate) {
- this.hiredate = hiredate;
- }
- public Double getSal() {
- return sal;
- }
- public void setSal(Double sal) {
- this.sal = sal;
- }
- public Double getComm() {
- return comm;
- }
- public void setComm(Double comm) {
- this.comm = comm;
- }
- public Integer getDeptno() {
- return deptno;
- }
- public void setDeptno(Integer deptno) {
- this.deptno = deptno;
- }
- @Override
- public String toString() {
- return "Emp{" +
- "empno=" + empno +
- ", ename='" + ename + '\'' +
- ", job='" + job + '\'' +
- ", mrg=" + mrg +
- ", hiredate=" + hiredate +
- ", sal=" + sal +
- ", comm=" + comm +
- ", deptno=" + deptno +
- '}';
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |