使用jdbc操作Oracle数据库
来源:互联网 发布:unity3d 水的特效 编辑:程序博客网 时间:2024/05/16 11:03
使用jdbc操作数据库步骤是固定的
1.将驱动包导入到数据库,每一个数据库的驱动包都不一样,下面我提供一个Oracle数据库的驱动包
http://download.csdn.net/detail/hncsy403/4530830
将它下载后放入web项目中的 web-inf中的lib中
2.选择项目右键,选择Build Bath,在libraries中add JARs,选择刚才的jar包
3.余下步骤示例如下:
package com.human.news;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;public class NewsDao {/** * @param args *///查询新闻信息public void getNewsList(){Connection connection=null;Statement stmt=null;ResultSet rs=null;try {//(1)Class.forName()加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");//(2)DriverManager.getConnection(URL,用户名,密码)获得数据库连接 (Connection)connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:news","news_dev_u","news");//(3)获得Statement对象,执行SQL语句String sql="select * from news_detail";stmt=connection.createStatement();rs=stmt.executeQuery(sql);//(4)处理执行结果(ResultSet),while(rs.next()){int id=rs.getInt("id");String title=rs.getString("title");String summary=rs.getString("summary");String content=rs.getString("content");String author=rs.getString("author");Timestamp time=rs.getTimestamp("createdate");System.out.println(id + "\t" + title + "\t" + summary + "\t"+ content + "\t" + author + "\t" + time);}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{//释放资源try {rs.close();stmt.close();connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {// TODO Auto-generated method stubNewsDao newsDao=new NewsDao();newsDao.getNewsList();}}
同时我们也可以把这些数据库配置数据放入一个配置文件中,例如我创建了一个database.properties文件在src中
jdbc.driver_class=oracle.jdbc.driver.OracleDriver
jdbc.connection.url=jdbc:oracle:thin:@localhost:1521:news
jdbc.connection.username=new_dev_u
jdbc.connection.password=news
这样修改数据库信息就可以在配置文件中操作,读取配置文件方法如下:
import java.io.IOException;import java.io.InputStream;import java.util.Properties;//读取配置文件(属性文件)的工具类public class ConfigManager {private static ConfigManager configManager;//properties.load(InputStream);读取属性文件private static Properties properties; private ConfigManager(){String configFile="database.properties";properties=new Properties();InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);try {properties.load(in);in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static ConfigManager getInstance(){if(configManager==null){configManager=new ConfigManager();}return configManager;}public String getString(String key){return properties.getProperty(key);}}
- 使用jdbc操作Oracle数据库
- 使用Java的JDBC操作Oracle数据库
- 使用JDBC操作数据库
- 使用JDBC操作数据库
- jdbc操作关系数据库(oracle)
- 通过JDBC操作ORACLE数据库
- 使用JDBC操作数据库(Oracle,Mysql,SQLSERVER)
- 使用JDBC连接oracle数据库
- 使用jdbc连接oracle数据库
- 使用Jdbc连接Oracle数据库
- 使用jdbc连接oracle数据库
- Spring使用JDBC操作数据库
- JAVA使用JDBC操作数据库
- 使用JDBC操作数据库(mysql):
- 使用jdbc操作mysql数据库
- 使用JDBC操作Mysql数据库
- Java使用JDBC操作数据库
- Java使用JDBC操作数据库
- XX is not in the sudoers file 问题解决【转载】
- 扪心第二想
- LDR指令、LDR伪指令、ADR伪指令区别
- 编译GWTproject时出现的问题
- VSS出错"Could not find the Visual SourceSafe Internet Web Service connection..."
- 使用jdbc操作Oracle数据库
- Mysql高可用方案
- Cannot create Windows Service for MySQL.Error:0 解决方法
- 什么是TTL电平和CMOS电平
- CCSprite效率优化
- 群赛54
- 赛艇数据采集整体框架
- WebService使用场景介绍
- Linux添加/删除用户和用户组(转载)