jdbc使用

数据库驱动util类:

package util;

import java.sql.Connection;       //导入Java。sql.connection类
import java.sql.DriverManager;    //导入java.sql.DriverManager类

public class DBHelper {
 private static final String driver="com.mysql.jdbc.Driver";     //数据库驱动
 private static final String url="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF8";      //连接数据库的url地址
    private static final String username="root";    //数据库的用户名
 private static final String password="root";    //数据库的密码
 
 private static Connection conn=null;
 //静态代码负责加载驱动
   static
   {
  try
  {
             Class.forName(driver);
     }
  catch(Exception ex)
  {
  ex.printStackTrace();
  }
 }
  
  
   //单例模式返回数据库连接对象
   public static Connection getConnection()throws Exception
   {
    if(conn==null)
    {
     conn=DriverManager.getConnection(url, username, password);
     return conn;
    }
    return conn;
   }
  
   //测试实例
   public static void main(String[] args){
   try
   {
  Connection conn=DBHelper.getConnection(); 
  if(conn!=null)
  {
  System.out.println("数据库连接正常"); 
  }
  else
  {
  System.out.println(); 
  }
   }
   catch(Exception ex)
   {
  System.out.println("数据库连接异常"); 
   }
   }
  
}

实体类:

package entity;                              //实体类
public class Items {
   private  int id;           //商品编号
   private String name;       //商品名称
   private String city;       //产地
   private int price;         //价格
   private int number;        //库存
   private String picture;    //商品图片
  
   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 getCity() {
   return city;
 }
 public void setCity(String city) {
   this.city = city;
 }
 public int getPrice() {
  return price;
 }
 public void setPrice(int price) {
  this.price = price;
 }
 public int getNumber() {
  return number;
 }
 public void setNumber(int number) {
  this.number = number;
 }
 public String getPicture() {
  return picture;
 }
 public void setPicture(String picture) {
  this.picture = picture;
 }
}

dao层:

package dao;

import java.sql.Connection;                 //导入java.sql.Connection类
import java.sql.PreparedStatement;          //导入java.sql.PreparedStatement类
import java.sql.ResultSet;                  //导入java.sql.ResultSet类
import java.sql.SQLException;               //导入java.sql.SQLException类
import java.util.ArrayList;                 //导入java.util.ArrayList类

import entity.Items;                        //导入entity.Items类
import util.DBHelper;                       //导入util.DBHelper类
 
//商品的业务逻辑类
public class ItemsDAO {
 //获得所有的商品信息
 public ArrayList<Items> getAllItems()     //定义方法
 {
  Connection conn=null;                 //connection    连接类
  PreparedStatement  stmt=null;         //PreparedStatement语句对象(.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
                                                       //(http://www.cnblogs.com/raymond19840709/archive/2008/10/13/1309657.html)

  ResultSet rs=null;                    //数据集
  ArrayList<Items> list=new ArrayList<Items>();     //新建一个list数组,商品集合
  try
  {
  conn=DBHelper.getConnection();      //获得一个连接对象
  String sql="select *from items";    //sql语句
  stmt=conn.prepareCall(sql);         //创建一个连接对象,未传入参数,直接将sql添加到preparcall中去
  rs=stmt.executeQuery();             //返回结果数据集ResultSet
  while(rs.next())
  {
   Items item=new Items();    //new一个items对象,将结果集中的数据赋给实例items,并添加到商品集中,返回list集合
   item.setId(rs.getInt("id"));
   item.setName(rs.getString("name"));
   item.setCity(rs.getString("city"));
   item.setNumber(rs.getInt("number"));
   item.setPrice(rs.getInt("price"));
   item.setPicture(rs.getString("picture"));
   list.add(item);     //添加到list商品集合中
  }
  return list;     //返回商品集合
  }
  catch(Exception ex)
  {
   return null;
  }
  finally{         //清理资源
   if(rs!=null)          
   {
   try {         //抛出异常,捕获   try catch
    rs.close();     //释放数据集对象
    rs=null;
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   }
   if(stmt!=null)
   {
    try {
     stmt.close();      //释放数据语句对象
     stmt=null;
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }    
   }
  }
  
 }
 
 
 //根据商品编号获取商品资料
public Items getItemsById(int id){
    Connection conn=null;
    PreparedStatement stmt=null;
    ResultSet rs=null;
    try{
       conn=DBHelper.getConnection();      //获得一个连接对象
       String sql="select *from items where id=?;";    //sql语句
       stmt=conn.prepareCall(sql);         //创建一个连接对象,未传入参数,直接将sql添加到preparcall中去
       stmt.setInt(1, id);                 //传入参数
      
       rs=stmt.executeQuery();             //返回结果数据集ResultSet
    if(rs.next())
    {
 Items item=new Items();    //new一个items对象,将结果集中的数据赋给实例items,并添加到商品集中
 item.setId(rs.getInt("id"));
 item.setName(rs.getString("name"));
 item.setCity(rs.getString("city"));
 item.setNumber(rs.getInt("number"));
 item.setPrice(rs.getInt("price"));
 item.setPicture(rs.getString("picture"));
 return item;    
    }
    else{
    return null;
        }
 }catch(Exception ex)
      {
           ex.printStackTrace();
           return null;         //抛异常返回null值,否知无返回值,系统报错
      }
finally{         //清理资源
 if(rs!=null)          
 {
 try {         //抛出异常,捕获   try catch
  rs.close();     //释放数据集对象
  rs=null;
 } catch (SQLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
 if(stmt!=null)
 {
  try {
   stmt.close();      //释放数据语句对象
   stmt=null;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }    
 }
  }
}
 
 //获取前五条商品信息
public ArrayList<Items> getViewList(String list)
{
 ArrayList<Items> itemsList=new ArrayList<Items>();    //new一个商品集合
 int iCount=5;   //每次返回前五条记录 
 if(list!=null&&list.length()>0)      //非空并且大于0
 {
  String[] arr=list.split(",");    //获取字符串数组,用split方法分割
  if(arr.length>=5){
  
  
  for(int i=arr.length-1;i>arr.length-iCount;i--)   //倒序
  {
   //如果商品记录>=5条
   itemsList.add(getItemsById(Integer.parseInt(arr[i])));      //添加一个商品明细 ,传入id  
  }
 }else{
  for(int i=arr.length-1;i>=0;i--){
   itemsList.add(getItemsById(1));
  }
 }
  return itemsList;
 }
 else
 {
  return null;
 }  

}

批处理文件:http://wenku.baidu.com/view/b1eaf4b669dc5022aaea0013.html

http://blog.csdn.net/yiyuhanmeng/article/details/8107493

原文地址:https://www.cnblogs.com/DZayxj520/p/6476662.html