jdbc链接数据库

获得数据库的连接

public class connectFactory {
    private static String name="";
    private static String user="";
    private static String password="";
    private static String url="";
      static Connection conn=null;

    static{
        name="oracle.jdbc.driver.OracleDriver";
        user="HQ133";
        password="HQ133";
        url="jdbc:oracle:thin:@localhost:1521:XE";
    }

    public static Connection getConnection(){
        if(conn==null){

            try {
                Class.forName(name);
                conn=DriverManager.getConnection(url, user, password);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return conn;
    }
    public static void main(String []args){
        Connection conn = connectFactory.getConnection();
        if(conn==null){
              System.out.println("--null--");   
        }else{
            System.out.println("--not null--");     
        }

}
}

对数据库进行增删改查操作

public class userDao {
    public int yanZheng(String name,String password){
        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int count=0;
        conn=connectFactory.getConnection();
        if(conn==null){
            System.out.println("ddddddd");
        }
        String sql="SELECT count(*) FROM users where name=? and password=?";

        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setString(2, password);
            rs=pstmt.executeQuery();
            if(rs.next()){
                count=rs.getInt(1);
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    count=0;
                }
            }

            if(pstmt!=null){

                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }
            }
            return count;

        }


   public List select(){
       Connection conn=null;
       PreparedStatement pstmt=null;
       ResultSet rs=null;

       List<Student> list=new ArrayList<Student>();
        conn=connectFactory.getConnection();
        String sql="SELECT * FROM info";
        try {
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while(rs.next()){
                Student stu=new Student();
                stu.setId(rs.getInt(1));
                stu.setName(rs.getString(2));
                stu.setAge(rs.getInt(3));
                stu.setSex(rs.getString(4));
                list.add(stu);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
       return list;
   }
   public int delete(int id){
      Connection conn=null;
      PreparedStatement pstmt=null;
         int count=0;
      conn=connectFactory.getConnection();
      String sql="DELETE FROM INFO WHERE id=?";
      try {
        pstmt=conn.prepareStatement(sql);
        pstmt.setInt(1,id);
        count=pstmt.executeUpdate();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return count;         

   }
   public int update(int id,String name,int age,String sex){
       Connection conn=null;
       PreparedStatement pstmt=null;
       int count=0;
       conn=connectFactory.getConnection();
       String sql="UPDATE info set name=?,age=?,sex=? WHERE id=?";
       try {
        pstmt=conn.prepareStatement(sql);
        pstmt.setString(1, name);
        pstmt.setInt(2, age);
        pstmt.setString(3, sex);
        pstmt.setInt(4, id);
        count=pstmt.executeUpdate();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
       return count;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值