JDBC

本文介绍了一个使用Java实现的Oracle数据库连接工厂类,并展示了如何通过该连接进行订单的增删改查等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

connection:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.0.202:1521:tarenadb
user=asd0606
password=asd0606

ConnectionFactory:

 

package jdbc;

import java.sql.*;
import java.util.*;
import java.io.*;

public class ConnectionFactory {
    
private static ThreadLocal<Connection> con=new ThreadLocal<Connection>();
    
    
private static String driver;
    
private static String url;
    
private static String user;
    
private static String password;

    
public static Connection getConnection() throws ClassNotFoundException,
            SQLException
{
        
if (con.get() == null || con.get().isClosed())
            initConnection();
        
return con.get();
    }


    
public static void initConnection() throws ClassNotFoundException,
            SQLException
{
        loadFromConfig();
   Class.forName(driver);
        con.set(DriverManager.getConnection(url, user,password));
        con.get().setAutoCommit(
false);
    }

    
public static void beginWorkUnit() throws SQLException{
        
        
//
}

    
public static void commitWorkUnit() throws SQLException{
        
if(existConnection())
            con.get().commit();
    }

    
public static void roolbackWorkUnit() throws SQLException{
        
if(existConnection())
            con.get().rollback();
    }

    
    
public static void release()throws SQLException{
        
if(existConnection())
            con.get().close();
    }

    
    
private static void loadFromConfig(){
        
try{
            Properties prop
=new Properties();
        
//从文件填充    Properties
        /*InputStream config=null;
     config=new FileInputStream("connection");
        prop.load(config);
        
*/

            ClassLoader classLoader
=ConnectionFactory.class.getClassLoader();
          InputStream config
=classLoader.getResourceAsStream("connection");
          prop.load(config);
        
//
        driver=prop.getProperty("driver");
        url
=prop.getProperty("url");
        user
=prop.getProperty("user");
        password
=prop.getProperty("password");
        }
catch(Exception e){
            e.printStackTrace();
        }

        
    }

    
private static boolean existConnection() throws SQLException{
        
return    con.get() != null&&!con.get().isClosed();
    }

}

OrderDAOImp:

 

package com.daosur;

import com.tarena.dao.OrderDAO;
import com.tarena.pojo.Order;
import com.tarena.pojo.OrderLine;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set;

import net.xiaopi.dao.common.*;


public class OrderDAOImp implements OrderDAO{

    
public void add(Order order) {
        PreparedStatement pstmt
=null;
        
try{
        String sql
="insert into Orders(id,code,user_id,shippeddate,sum) values (?,?,?,?,?)";
      pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
        pstmt.setInt(
1, order.getId());
        pstmt.setString(
2, order.getCode());
        pstmt.setInt(
3, order.getUser().getId());
        pstmt.setDate(
4new java.sql.Date((order.getShippedDate()).getTime()));
        pstmt.setFloat(
5, order.getSum());
        pstmt.executeUpdate();
        }
catch(Exception e){
            e.printStackTrace();
        }
finally{try{pstmt.close();}catch(Exception e){}
        }

    }


    
public void delete(Order order) {
        PreparedStatement pstmt
=null;
        
try{
            
if(order.getOrderLines()!=null)
            removeOderLinesByoid(order);
            String sql
="delete from orders where id=?";
            pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
            pstmt.setInt(
1, order.getId());
            pstmt.executeUpdate();
        }
catch(Exception e){
            e.printStackTrace();
        }
finally{try{pstmt.close();}catch(Exception e){}
        }

        
    }


    
public Order load(Integer id) {
        Order order
=new Order();
        UsersDAOImp usersdi
=new UsersDAOImp();
        PreparedStatement pstmt
=null;
        
try{
            String sql
="select id,code,user_id,shippeddate,sum from orders where id=?";
            pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
            pstmt.setInt(
1, id);
            order.setId(id);
            ResultSet rs
=pstmt.executeQuery();
            Set
<OrderLine> set= displayOrderlinesByoid(id);
            order.setOrderLines(set);
            
if(rs.next()){
                order.setCode(rs.getString(
"code"));
                order.setUser(usersdi.load(rs.getInt(
"user_id")));
                order.setShippedDate(rs.getDate(
"shippeddate"));
                order.setSum(rs.getFloat(
"sum"));
                }

        }
catch(Exception e){
            e.printStackTrace();
        }
finally{try{pstmt.close();}catch(Exception e){}
        }

        
        
return order;
    }


    
public void save(Order order) {
        PreparedStatement pstmt
=null;
        
try{
            String sql
="update orders set code=?,user_id=?,shippeddate=?,sum=? where id=?";
            pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
            pstmt.setString(
1, order.getCode());
            pstmt.setInt(
2, order.getUser().getId());
            pstmt.setDate(
3new java.sql.Date((order.getShippedDate()).getTime()));
            pstmt.setFloat(
4, order.getSum());
            pstmt.executeUpdate();
        }
catch(Exception e){
            e.printStackTrace();
        }
finally{try{pstmt.close();}catch(Exception e){}
        }

        
    }

    
private static Set<OrderLine> displayOrderlinesByoid(Integer id){
        OrderLine orderlines
=new OrderLine();
        ProductDAOImp prod
=new ProductDAOImp();
        Set
<OrderLine> list=new HashSet<OrderLine>();
        PreparedStatement pstmt
=null;
        
try{
            String sql
="select id, product_id,quantity,total from orderlines where order_id=?";
            pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
            pstmt.setInt(
1, id);
            ResultSet rs
=pstmt.executeQuery();
            
while(rs.next()){
                orderlines.setId(rs.getInt(
"id"));
                orderlines.setProduct(prod.load(rs.getInt(
"product_id")));
                orderlines.setQuantity(rs.getInt(
"quantity"));
                orderlines.setTotal(rs.getFloat(
"total"));
                list.add(orderlines);
            }

        }
catch(Exception e){
            e.printStackTrace();
        }
finally{try{pstmt.close();}catch(Exception e){}
        }

        
return list;
    }

private static void removeOderLinesByoid(Order order){
    PreparedStatement pstmt
=null;
    
try{
        String sql
="delete from orderlines where order_id=?";
        pstmt
=ConnectionFactory.getConnection().prepareStatement(sql);
        pstmt.setInt(
1, order.getId());
        pstmt.executeUpdate();
    }
catch(Exception e){
        e.printStackTrace();
    }
finally{try{pstmt.close();}catch(Exception e){}
    }

}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值