java连接Oracle数据库实现增删改查并在Navicat中显示

Posted 旁光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java连接Oracle数据库实现增删改查并在Navicat中显示相关的知识,希望对你有一定的参考价值。

创建TEST表

eclipse中的java项目

 

代码

数据库方法类 DBUtil:

package util;

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 USERNAMR = "system";
         String PASSWORD = "*****";//自己的密码
         String DRVIER = "oracle.jdbc.OracleDriver";
         String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
        try {
            Class.forName(DRVIER);
            connection = DriverManager.getConnection(URL, USERNAMR, 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 connection ) {
		try {
			if (connection != null) {
				connection.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement preparedStatement ) {
		try {
			if (preparedStatement != null) {
				preparedStatement.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void close(ResultSet resultSet ) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

实体类 Model

package test;

public class Model {
    private String id;

    private String username;
    private String password;
    private String name;
    private int age;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Model()
    {
        
    }
    public Model(String id, String username, String password, String name, int age) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.age = age;
    }
    public void update(String username, String password, String name, int age) {
        this.username = username;
        this.password = password;
        this.name = name;
        this.age = age;
    }
    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 String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

测试类 Test

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


import util.DBUtil;

public class Test {
    public void add(Model model)//添加数据
    {
        Connection connection=DBUtil.getConnection();
        String sql="insert into test(id,username,password,name,age)values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, model.getId());
            preparedStatement.setString(2, model.getUsername());
            preparedStatement.setString(3, model.getPassword());
            preparedStatement.setString(4, model.getName());
            preparedStatement.setInt(5, model.getAge());
            preparedStatement.executeUpdate();
            System.out.println("插入成功");
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally{
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }
    public void update(Model model)//修改数据
    {
        Connection connection=DBUtil.getConnection();
        String sql="update test set username=?,password=?,name=?,age=?";
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, model.getUsername());
            preparedStatement.setString(2, model.getPassword());
            preparedStatement.setString(3, model.getName());
            preparedStatement.setInt(4, model.getAge());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally{
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }
    public Model load(String id) //查询数据
    {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from test  where id = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Model model = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                model = new Model();
                model.setId(id);
                model.setUsername(resultSet.getString("username"));
                model.setPassword(resultSet.getString("password"));
                model.setName(resultSet.getString("name"));
                model.setAge(resultSet.getInt("age"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  model;
    }
    public void delete(String id)//删除数据
    {
        Connection connection=DBUtil.getConnection();
        String sql="delete from test where id=?";
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            preparedStatement.executeQuery();
            System.out.println("删除成功");
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }
    
    public static void main(String args[])
    {
        Test test=new Test();
        Model model=new Model("1","123","123","张三",18);
//        test.add(model);
        
        
//        model.update("123","123","张三",28);
//        test.update(model);
//        
//        model=test.load("1");
//        System.out.println("查询结果————姓名:"+model.getName()+",年龄:"+model.getAge());
//        
        test.delete("1");
    }
}

用Navicat连接Oracle数据库,因为创建的表自动存放在SYSTEM表空间内,在SYSTEM下边可以直接找到。

 

 

Navicat中的数据变化:

插入数据。

更改后的数据。

删除数据。

 

以上是关于java连接Oracle数据库实现增删改查并在Navicat中显示的主要内容,如果未能解决你的问题,请参考以下文章

用java做界面连接oracle数据库实现增删改查功能,具体步骤怎么做?

java连接sql数据库,怎么实现增删改查?

求.net 连ORACLE数据库 增删改查完整代码 代码 要最简单的

java web 中连接数据库并在HTML上进行增删改查

Java操作Oracle并实现增删改查

java 连接oracle 进行增删改查