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 ...
随机推荐
- linux awk 内置函数详细介绍(实例)
这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...
- PRmakefile文件
Ubuntu下的makefile: # /******************************************************************************* ...
- mask r-cnn
mask R-cnn, kaiming he的新作.可以同时完成object detection和segmentation,还可以做pose estimation,简直就是功能多多啊.在coco上测试 ...
- django+xadmin在线教育平台(十二)
6-4 用form实现登录-1 上面我们的用户登录的方法是基于函数来做的.本节我们做一个基于类方法的版本. 要求对类的继承有了解. 基础教程中基本上都是基于函数来做的,其实更推荐基于类来做.基于类可以 ...
- percona-zabbix-templates插件安装监控MySQL
前期准备:被监控机已经安装好php 1.在zabbix客户端安装mysql监控插件rpm包 rpm -ivh https://www.percona.com/downloads/percona-mon ...
- 交换机基础配置之三层交换机实现vlan间通信
我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...
- 交换机基础配置之单交换机划分vlan
我们以以上拓扑图为例 pc0的IP地址为:192.168.1.1 pc1的ip地址为:192.168.1.2 两台主机在同一网段,相互ping是能ping通的 我们的目的是在单交换机上划分两个vlan ...
- centOS初了解--***安装node
在***买了一个VPS,用了差不多一年了,除了做FQ使用之外,同时也下载了一个node,用了express搭建了一个服务,同时我在博客园有博客,我也懒得转来转去了,直接做了一个重定向,跳转到了博客园. ...
- python json.dumps raise TypeError(repr(o) + " is not JSON serializable") TypeError: 0 is not JSON serializable
出错如题. 这个问题有可能是因为python的json.dumps没法识别dump内容里的某些数据类型导致的.我的问题是因为dict中含有numpy.int64,numpy.float等类型导致的,需 ...
- php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数
// opcode处理器 --- ZEND_DO_FCALL_SPEC_CONST_HANDLER实现在 php-5.6.26\Zend\zend_vm_execute.h static int ZE ...