• Java连接Oracle数据库示例


    1.数据库复用模块

    package cn.jzy.database;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBFcatory {
    	Connection conn ;
    	public DBFcatory(){
    		getConnection();
    	}
    	private Connection getConnection() {
    		conn=null;
    		try {
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    			conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:jzy","jzy","123456");
    		
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return conn;
    	}
    	//增,删,改操作
    	public void OracleExecute(String sql,Object[] params){
    		try {
    			if(conn==null){
    				conn=getConnection();
    			}
    			PreparedStatement pstm=conn.prepareStatement(sql);
    			for(int i=0;i<params.length;i++){
    				pstm.setObject(i+1, params[i]);
    			}
    			pstm.execute();
    			closeConnection();
    			System.out.println("执行成功");
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public ResultSet OracleExecuteQuery(String sql,Object[] params){
    		ResultSet rs=null;
    		try {
    			if(conn==null){
    				conn=getConnection();
    			}
    			PreparedStatement pstm=conn.prepareStatement(sql);
    			for(int i=0;i<params.length;i++){
    				pstm.setObject(i+1, params[i]);
    			}
    			rs=pstm.executeQuery();		
    			System.out.println("查找成功");
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		return rs;
    	}
    	
    	public void closeConnection(){
    		try {
    			if(conn!=null){
    				conn.close();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			conn=null;
    		}
    	}
    }
    

    2.数据库测试模块

    package cn.jzy.database;
    
    import java.sql.ResultSet;
    
    public class TestDBFActory {
    
    	public static void main(String[] args) {
    		DBFcatory dbf=new DBFcatory();
    		String sql="insert into emp values (seq_emp.Nextval,?,?,?,?,?,?,?,?)";
    		Object[] params={1,"小五",20,"女","员工","1",5000,"成都"};
    		dbf.OracleExecute(sql, params);
    		
    		String sql2="select * from emp where id=?";
    		Object[] params2={1};
    		try{
    		ResultSet rs=dbf.OracleExecuteQuery(sql2, params2);
    		while(rs.next()){
    			System.out.println(rs.getString("name"));
    		}
    		dbf.closeConnection();
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    
    }
    


  • 相关阅读:
    迭代是人,递归是神(迭代与递归的总结:比较)
    HMM(隐马尔科夫)用于中文分词
    Python 字典(Dictionary) get()方法
    jieba中文分词源码分析(四)
    一个隐马尔科夫模型的应用实例:中文分词
    小白给小白详解维特比算法(二)
    IOS 播放音频流媒体
    iOS开发之多媒体API(1)
    IOS流媒体播放
    ios7中使用scrollview来横向滑动图片,自动产生偏移竖向的偏移 问题
  • 原文地址:https://www.cnblogs.com/james1207/p/3257900.html
Copyright © 2020-2023  润新知