Jdbc封装技术

package org.leadfar;

import java.util.Date;

public class Student {
 private int id;
 private String name;
 private String password;
 private int age;
 private Date birthday;
 private String sex;
 private String memo;
 private String photo;
 private Date reg_times;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public Date getBirthday() {
  return birthday;
 }

 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }

 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }

 public String getMemo() {
  return memo;
 }

 public void setMemo(String memo) {
  this.memo = memo;
 }

 public String getPhoto() {
  return photo;
 }

 public void setPhoto(String photo) {
  this.photo = photo;
 }

 public Date getReg_times() {
  return reg_times;
 }

 public void setReg_times(Date regTimes) {
  reg_times = regTimes;
 }

}

 

=================================================================================

package org.leadfar;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {

 public static Connection getconn() {
  try {
   // 1.加载驱动
   Class.forName("oracle.jdbc.OracleDriver");

  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   System.out.println("驱动加载失败");
  }
  // 2.得到连接
  String url = "jdbc:oracle:thin:@localhost:1521:leadfar";
  Connection conn = null;
  try {
   conn = DriverManager.getConnection(url, "BBS", "BBS");
   System.out.println("成功!");
   conn.setAutoCommit(false);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   System.out.println("数据库连接失败");
  }
  return conn;
 }
 
 public   static void  close(Connection conn) {
  if(conn!=null){
   try {
    conn.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  
 }

  
 
 
 public  static void close(PreparedStatement pstatement){
  if(pstatement!=null){
   try {
    pstatement.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }
 
 public static void  close(ResultSet rs){
  if(rs!=null){
   try {
    rs.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  
 }
 
 public static  void commit(Connection conn){
  if(conn!=null){
   try {
    conn.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  
 }
 public static void rollback(Connection conn) {
  try {
   if (conn != null) {
    conn.rollback();
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
}
}

 

=============================================================================

 

package org.leadfar;

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

public class StudentDao {

 public void save(Student stu) {
  Connection conn = DB.getconn();
  PreparedStatement pstatement = null;
  String sql = "insert into t_student(name,password,age,birthday,sex,memo,photo,reg_time)";
  sql += "values (?,?,?,?,?,?,null,null)";

  try {
   pstatement = conn.prepareStatement(sql);
   pstatement.setString(1, stu.getName());
   pstatement.setString(2, stu.getPassword());
   pstatement.setInt(3, stu.getAge());
   pstatement.setDate(4,
     new java.sql.Date(stu.getBirthday().getTime()));
   pstatement.setString(5, stu.getSex());
   pstatement.setString(6, stu.getMemo());
   /*try {
    // 构建一个输入流存blob
    pstatement.setBlob(7, (Blob)new FileInputStream(stu.getPhoto()));
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
   pstatement.setTimestamp(8, new java.sql.Timestamp(stu
     .getReg_times().getTime()));
     */
   pstatement.executeUpdate();
   DB.commit(conn);

  } catch (SQLException e) {
   DB.rollback(conn);
   e.printStackTrace();

  } finally {

   DB.close(pstatement);
   DB.close(conn);

  }

 }

 public void delete(int id) {
  Connection conn = DB.getconn();
  PreparedStatement pstatement = null;
  String sql2 = "delete from t_student where id=?";
  try {
   pstatement = conn.prepareStatement(sql2);
   pstatement.setInt(1, id);
   pstatement.executeUpdate();
   DB.commit(conn);
  } catch (SQLException e) {
   DB.rollback(conn);
   e.printStackTrace();
  } finally {
   DB.close(pstatement);
   DB.close(conn);
  }

 }

 public void delete(int[] id) {

 }

 public void get(int id) {

 }

 public List<Student> query() {

  return null;
 }

}

 

 

===============================================================

 

package org.leadfar;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
 
  Student s=new Student();
  s.setId(2);
  s.setName("杨幂");
  s.setPassword("ym");
  s.setAge(23);
  try {
   s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1980-01-01"));
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (java.text.ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  s.setSex("女");
  s.setMemo("这是一个小美女");
  s.setPhoto("c:\\sa.jpg");
  s.setReg_times(new Date());
  StudentDao sd=new StudentDao();
  sd.save(s);
  //sd.delete(1);
  
  
 }

}

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值