数据库连接类及数据检索 最基本的数据库操作实例

源代码在线查看: addressbookbean.java

软件大小: 2 K
上传用户: jill
关键词: 数据库 数据 连接 检索
下载地址: 免注册下载 普通下载 VIP

相关代码

				package dbproject.model;
				
				import java.sql.*;
				import java.util.*;
				import dbproject.Constants;
				
				public class AddressBookBean {
				
				  String name;
				  String phone;
				  String address;
				
				  public AddressBookBean() {
				  }
				  public AddressBookBean(String name,String phone,String address) {
				    this.name=name;
				    this.phone=phone;
				    this.address=address;
				  }
				  public String getName(){
				    return name;
				  }
				  public String getPhone(){
				    return phone;
				  }
				  public String getAddress(){
				    return address;
				  }
				  public void setName(String name){
				    this.name=name;
				  }
				  public void setPhone(String phone){
				    this.phone=phone;
				  }
				  public void setAddress(String address){
				    this.address=address;
				  }
				
				  public void insert()throws Exception{
				      Connection con= DbUtil.connectToDb();
				      PreparedStatement pStmt=null;
				      try{
				              pStmt=con.prepareStatement("INSERT INTO " + Constants.TABLENAME +
				                                              " (name,phone,address)"+
				                                              " values(?,?,?)");
				              con.setAutoCommit(false);
				
				              pStmt.setString(1,name);
				              pStmt.setString(2,phone);
				              pStmt.setString(3,address);
				              int j=pStmt.executeUpdate();
				              con.commit();
				        }
				      catch(Exception ex)
				      {
				              try{
				                      con.rollback();
				                }catch(SQLException sqlex){
				                      sqlex.printStackTrace(System.out);
				              }
				              throw ex;
				      }finally{
				        try{
				          pStmt.close();
				          con.close();
				        }catch(Exception e){e.printStackTrace();}
				      }
				
				  }
				  public static Vector search(String strSql)throws Exception{
				      Vector addressbookBeans=new Vector();
				      Connection con= DbUtil.connectToDb();
				      PreparedStatement pStmt=null;
				      ResultSet rs=null;
				      try{
				        pStmt=con.prepareStatement(strSql);
				        rs=pStmt.executeQuery();
				        while(rs.next())
				          addressbookBeans.add(new AddressBookBean(
				          rs.getString("NAME"),rs.getString("PHONE"),rs.getString("ADDRESS")));
				
				        return addressbookBeans;
				      }finally{
				          try{
				            rs.close();
				            pStmt.close();
				            con.close();
				          }catch(Exception e){e.printStackTrace();}
				      }
				   }
				}
							

相关资源