zoukankan      html  css  js  c++  java
  • oracle---jdbc--laobai

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    //自定义jdbc工具类。
    //目的是简化jdbc开发。
    public class JDBCUtil {
    	public static String driver="oracle.jdbc.driver.OracleDriver";
    	public static String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    	public static String user = "system";
    	public static String password = "orcl";
    	//获取1条数据库的连接	
    	public static Connection getConnection()
    	{
    		Connection con = null;  //返回的连接
    		try {
    			//1 加载驱动
    			Class.forName(driver);
    			con = DriverManager.getConnection(url, user, password);
    			//2 获取连接
    		} catch (Exception e) 
    		{
    			e.printStackTrace();
    		}
    		return con;
    	}
    	//查询:简单查询,不含?的sql语句
    	public static  ResultSet doQuery(String sql)
    	{
    		return doQuery(sql,null);
    	}
    
    	//查询:复杂查询,含有多个?的sql语句
    	public static  ResultSet doQuery(String sql,String... ps)
    	{
    		//1 获取连接
    		Connection con = getConnection(); //调用上面的方法,获取1个连接
    		if(con==null)
    		{
    			System.out.println("获取连接失败!");
    			return null; //提前终止本方法代码 
    		}
    
    		ResultSet rs = null;
    		try {
    			//2执行查询
    			PreparedStatement psm = con.prepareStatement(sql);
    			//通过for循环,访问参数数组,给psm逐个设置可变参数!!
    			if(ps!=null) //只有可变参数不为空,才进行?赋值
    			{
    				for(int i=0;i<ps.length;i++)
    				{
    					psm.setString(i+1, ps[i]);//将第i个可变参数,设置到第i+1个问号
    				}
    			}
    			rs = psm.executeQuery();
    		} catch (Exception e) 
    		{
    			e.printStackTrace();
    		}
    		//3 返回结果.注意,不能关闭,否则结果用不了
    		return rs;
    	}
    
    	//修改(包含添加,删除,修改,删表,建表):返回sql影响的行数.不含?的sql语句
    	public static int doUpdate(String sql)
    	{
    		return doUpdate(sql,null);
    	}
    
    	//修改(包含添加,删除,修改,删表,建表):返回sql影响的行数.含?有的sql语句
    	public static int doUpdate(String sql,String... ps)
    	{
    		//1 获取连接
    		Connection con = getConnection(); //调用上面的方法,获取1个连接
    		if(con==null)
    		{
    			System.out.println("获取连接失败!");
    			return 0; //提前终止本方法代码 
    		}
    
    		int result = 0;
    		PreparedStatement psm = null;
    		try {
    			//2执行查询
    			psm = con.prepareStatement(sql);
    			//通过for循环,访问参数数组,给psm逐个设置可变参数!!
    			if(ps!=null)
    			{
    				for(int i=0;i<ps.length;i++)
    				{
    					psm.setString(i+1, ps[i]);//将第i个可变参数,设置到第i+1个问号
    				}
    			}
    			result = psm.executeUpdate();
    		} catch (Exception e) 
    		{			
    			e.printStackTrace();
    		}
    		finally
    		{
    			close(null,psm,con); //修改完毕,记得关闭资源
    		}
    		//3 返回结果.注意,不能关闭,否则结果用不了
    		return result;
    
    	}
    
    	//关闭资源。三个参数的版本
    	public static void close(ResultSet rs,Statement sm,Connection con)
    	{
    		try {
    			if (rs != null)
    				rs.close();
    			if (sm != null)
    				sm.close();
    			if (con != null)
    				con.close();
    		} catch (Exception e) 
    		{		
    			e.printStackTrace();
    		}		
    	}
    	//关闭资源。一个参数的版本
    	public static void close(ResultSet rs)
    	{
    		Statement sm = null;
    		Connection con = null;
    		try {
    			if (rs != null)
    			{
    				sm = rs.getStatement();
    				rs.close();
    			}
    
    			if (sm != null)
    			{
    				con = sm.getConnection();
    				sm.close();
    			}			
    			if (con != null)
    				con.close();
    		} catch (Exception e) 
    		{		
    			e.printStackTrace();
    		}		
    	}
    }
    

      

  • 相关阅读:
    Window 窗口类
    使用 Bolt 实现 GridView 表格控件
    lua的table库
    Windows编程总结之 DLL
    lua 打印 table 拷贝table
    使用 xlue 实现简单 listbox 控件
    使用 xlue 实现 tips
    extern “C”
    COleVariant如何转换为int double string cstring
    原来WIN32 API也有GetOpenFileName函数
  • 原文地址:https://www.cnblogs.com/ipetergo/p/6251023.html
Copyright © 2011-2022 走看看