java连接Oracle数据库的方法解析
程序员文章站
2024-03-11 15:30:07
oracle数据库先创建一个表和添加一些数据
1.先在oracle数据库中创建一个student表:
create table student
(
id...
oracle数据库先创建一个表和添加一些数据
1.先在oracle数据库中创建一个student表:
create table student ( id number(11) not null primary key, stu_name varchar(16) not null, gender number(11) default null, age number(11) default null, address varchar(128) default null );
2.向表中增添一些数据
insert into student values('1','王小军','1','17','北京市和平里七区30号楼7门102')
myeclipse里编写java代码
1.将ojdbc6.jar导入项目中
先创建一个项目,然后在鼠标移到项目上右键-->new-->folder;folder name:lib;这样就在项目中创建了一个文件夹lib;然后将ojdbc6.jar包导入该文件夹中
该包下载地址链接:
鼠标移到该包上;右键-->build path-->add to build path;
2.创建一个类,开始编码
import java.sql.connection; import java.sql.drivermanager; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.resultsetmetadata; import java.sql.sqlexception; public class operateoracle { // 定义连接所需的字符串 // 192.168.0.x是本机地址(要改成自己的ip地址),1521端口号,xe是精简版oracle的默认数据库名 private static string usernamr = "orcl"; private static string password = "orcl"; private static string drvier = "oracle.jdbc.oracledriver"; private static string url = "jdbc:oracle:thin:@192.168.0.x:1521:xe"; // 创建一个数据库连接 connection connection = null; // 创建预编译语句对象,一般都是用这个而不用statement preparedstatement pstm = null; // 创建一个结果集对象 resultset rs = null; /** * 向数据库中增加数据 * 首先获取表内数据总数,总数+1为新增数据的id值 * @param stuname:学生姓名 * @param gender:学生性别,1表示男性,2表示女性 * @param age:学生年龄 * @param address:学生住址 */ public void adddata(string stuname, int gender, int age, string address) { connection = getconnection(); // string sql = // "insert into student values('1','王小军','1','17','北京市和平里七区30号楼7门102')"; string sql = "select count(*) from student where 1 = 1"; string sqlstr = "insert into student values(?,?,?,?,?)"; int count = 0; try { // 计算数据库student表中数据总数 pstm = connection.preparestatement(sql); rs = pstm.executequery(); while (rs.next()) { count = rs.getint(1) + 1; system.out.println(rs.getint(1)); } // 执行插入数据操作 pstm = connection.preparestatement(sqlstr); pstm.setint(1, count); pstm.setstring(2, stuname); pstm.setint(3, gender); pstm.setint(4, age); pstm.setstring(5, address); pstm.executeupdate(); } catch (sqlexception e) { e.printstacktrace(); } finally { releaseresource(); } } /** * 向数据库中删除数据 * @param stuname:根据姓名删除数据 */ public void deletedata(string stuname) { connection = getconnection(); string sqlstr = "delete from student where stu_name=?"; system.out.println(stuname); try { // 执行删除数据操作 pstm = connection.preparestatement(sqlstr); pstm.setstring(1, stuname); pstm.executeupdate(); } catch (sqlexception e) { e.printstacktrace(); } finally { releaseresource(); } } /** * 向数据库中修改数据 * @param stuname:学生姓名,根据此值查询要修改的某行值 * @param gender * @param age * @param address */ public void updatedata(string stuname, int gender, int age, string address) { connection = getconnection(); string sql = "select id from student where 1 = 1 and stu_name = ?"; string sqlstr = "update student set stu_name=?,gender=?,age=?,address=? where id=?"; int count = 0; try { // 计算数据库student表中数据总数 pstm = connection.preparestatement(sql); pstm.setstring(1, stuname); rs = pstm.executequery(); while (rs.next()) { count = rs.getint(1); system.out.println(rs.getint(1)); } // 执行插入数据操作 pstm = connection.preparestatement(sqlstr); pstm.setstring(1, stuname); pstm.setint(2, gender); pstm.setint(3, age); pstm.setstring(4, address); pstm.setint(5, count); pstm.executeupdate(); } catch (sqlexception e) { e.printstacktrace(); } finally { releaseresource(); } } /** * 向数据库中查询数据 */ public void selectdata() { connection = getconnection(); string sql = "select * from student where 1 = 1"; try { pstm = connection.preparestatement(sql); rs = pstm.executequery(); while (rs.next()) { string id = rs.getstring("id"); string name = rs.getstring("stu_name"); string gender = rs.getstring("gender"); string age = rs.getstring("age"); string address = rs.getstring("address"); system.out.println(id + "\t" + name + "\t" + gender + "\t" + age + "\t" + address); } } catch (sqlexception e) { e.printstacktrace(); } finally { releaseresource(); } } /** * 使用resultsetmetadata计算列数 */ public void selectdata2() { connection = getconnection(); string sql = "select * from employees where 1 = 1"; int count = 0; try { pstm = connection.preparestatement(sql); rs = pstm.executequery(); while (rs.next()) { count++; } resultsetmetadata rsmd = rs.getmetadata(); int cols_len = rsmd.getcolumncount(); system.out.println("count=" + count + "\tcols_len=" + cols_len); } catch (sqlexception e) { e.printstacktrace(); } finally { releaseresource(); } } /** * 获取connection对象 * * @return */ public connection getconnection() { try { class.forname(drvier); connection = drivermanager.getconnection(url, usernamr, password); system.out.println("成功连接数据库"); } catch (classnotfoundexception e) { throw new runtimeexception("class not find !", e); } catch (sqlexception e) { throw new runtimeexception("get connection error!", e); } return connection; } /** * 释放资源 */ public void releaseresource() { if (rs != null) { try { rs.close(); } catch (sqlexception e) { e.printstacktrace(); } } if (pstm != null) { try { pstm.close(); } catch (sqlexception e) { e.printstacktrace(); } } if (connection != null) { try { connection.close(); } catch (sqlexception e) { e.printstacktrace(); } } } }
3.创建一个测试类
public class test { public static void main(string[] args) { /** * 增删改查完成,但是有一定局限性 * 1.增 问题不大 * 2.删 要给出一个值去删除(可能值不存在-->没有处理机制,值不唯一怎么处理?) * 3.改 同删的问题 * 4.查 问题不大 */ //创建operateoracle对象 operateoracle oo=new operateoracle(); //测试增加数据操作 //oo.adddata("孙中山",1,25,"北京市海淀区红旗路111号"); //测试删除数据操作 //oo.deletedata("孙中山"); //测试更新数据操作 oo.updatedata("孙中山",1,30,"北京市东城区岳山路11号"); //测试查询数据操作 //oo.selectdata(); //测试resultsetmetadata类 //oo.selectdata2(); } }
正如测试类中所注释的,此处只可按照正确的方式去连接oracle数据库,操作增删改查操作,但是对于一些错误操作的处理机制还不够完善。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!