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 jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- 通过jdbc连接MySql数据库的增删改查操作
一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...
- SSMybatis整合 --详细解读Mybatis对oracle数据库进行增删改查(一)
Mybatis是现在主流的持久化层框架,与Hibernate不同的是,它鼓励程序员使用原声SQL语句对数据库进行操作.因此提供了非常灵活的功能.特别是当数据库同时访问数过多,需要进行优化时,使用sql ...
- sql server连接oracle并实现增删改查
需要一个软件ODAC112040Xcopy_64bit 我连接的oracle是11g r2 sqlserver 是 2016 软件下载 https://pan.baidu.com/s/1OpYmpR ...
- C++ API方式连接mysql数据库实现增删改查
这里复制的 http://www.bitscn.com/pdb/mysql/201407/226252.html 一.环境配置 1,装好mysql,新建一个C++控制台工程(从最简单的弄起,这个会了, ...
- 【C#】使用NHibernate连接MySQL数据库及增删改查
学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...
- c#连接Access数据库及增删改查作
access:版本2003(后缀.mdb,新版access可另存为2003兼容版) using: using System;using System.Data;using System.Windows ...
- 使用nodejs连接mysql数据库实现增删改查
首先要有数据库 使用xampp 或者 phpstudy 可以傻瓜式安装 新建一个项目文件夹 之后在这个目录下初始化package.json (npm init) 先在项目中安装mysql 和 ex ...
- 使用NHibernate连接MySQL数据库及增删改查
学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...
随机推荐
- 控制元素的div属性
1.需求分析 改变元素的宽.高.颜色.显示.重置等属性. 2.技术分析 基础的css.html.js 3.详细分析 如图,单击按钮,改变元素属性: 3.1 HTML部分 根据视图不难发现,内容分两大不 ...
- docker swarm使用keepalived+haproxy搭建基于percona-xtradb-cluster方案的高可用mysql集群
一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...
- spring boot+log4j2快速使用(一)
log4j是Apache的一个开源项目,log4j2和log4j是一个作者,只不过log4j2是重新架构的一款日志组件,他抛弃了之前log4j的不足,以及吸取了优秀的logback的设计重新推出的一款 ...
- ntp网络时间服务搭建
1.1 NTP简介 NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议. 1.2 NTP用途 有些时候,局域网里面的设备需要进行时间的同步, ...
- MySQL字段属性介绍
引言 这次Qi号分享MySQL字段属性简介.下面资料是Qi号搜集大量资料与个人理解的整理. MySQL提供了一组可以赋给表中各个列的数据类型,每个类型都强制数据满足为该数据类型预先确定的一组规则,例如 ...
- __builtin_popcount() 函数
详解 该函数的主要作用是计算一个数字的二进制中有多少个1,返回值就是其中1的个数. 它使用一张基于表的方法来进行位搜索,因此这个操作的执行效率很高 此处举一题 P1582 倒水 #include &l ...
- Permute Digits 915C
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...
- python读取文件
请参考:http://www.cnblogs.com/sysuoyj/archive/2012/03/14/2395789.html
- 13,发布CRM
发布CRM你将使用以下软件 nginx uWSGI CentOS7 CRM项目文件 virtualenv supervisor WSGI.uWSGI python web服务器开发使用WSGI协议(W ...
- WCF入门一[WCF概述]
一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...