JAVA连接ORACLE数据库方法及测试
import java.sql.*; public class Bc { // 声明数据库地址及驱动 private Connection conn = null; private String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; private String driver = "oracle.jdbc.driver.OracleDriver"; private String user = "www"; private String password = "www"; // 主函数 public static void main(String[] args) { Bc bc = new Bc(); String sql="select * from T_USERS"; String sql1 = "insert into T_USERS values ('rrr','ooo')"; String sql2 ="update T_USERS set USERSPASSWORD = '888888888884444' where USERSNAME= 'www'"; ResultSet rs= bc.select(sql); bc.execute(sql1); bc.execute(sql2); try{ //显示查询出来的结果------------------ ResultSetMetaData rmeta = rs.getMetaData(); //获得数据字段个数 int numColumns = rmeta.getColumnCount(); while(rs.next()) { for(int i = 0;i< numColumns;i++) { String sTemp = rs.getString(i+1); System.out.print(sTemp+" "); } System.out.println(""); } } catch (Exception e) { // TODO: handle exception } } // 创建数据库连接方法 public Connection create() { try { /* 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它 */ Class.forName(driver); /* 通过DriverManager的getConnection()方法获取数据库连接 */ conn = DriverManager.getConnection(url, user, password); } catch (Exception ex) { System.out.println("数据库连接出错"); } return conn; } // 执行查询时用的方法 public ResultSet select(String sql) { Connection c = create();//获取连接对象,可以不声明 ResultSet rs = null; try { Statement st = c.createStatement();// 获取Staetment对象 rs = st.executeQuery(sql);// 执行操作 } catch (Exception e) { System.out.println("查询出错"); } return rs; } // 更新方法 public void execute(String sql) { Connection c = create();//获取连接对象,可以不声明 ResultSet rs = null; try { Statement st = c.createStatement();// 获取Staetment对象 st.executeUpdate(sql); } catch (Exception e) { System.out.println("更新出错"); } } }
- 粉丝: 3
- 资源: 7
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
评论0