【JSP开发】数据库连接与增删改查

一直都没有总结数据库连接与增删改查,今天做一些相关的小总结


JSP有自己的包的规范,要把每一个具体的功能的类放在相应的包下。

有关数据库的类都放在cn.hpu.bbs.util包下,对数据库操作的类放在cn.hpu.bbs.service包下,JavaBean放在cn.hpu.bbs.model包下。

我连接的是Mysql,事先要在工程中引入相应的jar包:mysql-connector-java-3.1.13-bin.jar


创建数据库:



数据库连接类:

package cn.hpu.bbs.util;

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

public class DB {
 
 // 定义MySQL的数据库驱动程序
 public static final String DBDRIVER = "com.mysql.jdbc.Driver" ;
 //定义mysql的数据库连接地址:
 public static final String DBDURL = "jdbc:mysql://localhost/bbs2014" ;
 //mysql数据库的连接用户名
 public static final String DBUSER = "root" ;
 //mysql数据库的连接密码
 public static final String DBPASS = "1234" ;
 
 public static Connection createConn(){
  Connection conn =null;
  try {
   Class.forName(DBDRIVER);
   conn=DriverManager.getConnection(DBDURL,DBUSER,DBPASS);
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  
  return conn;
 }
 
 public static PreparedStatement prepare(Connection conn,String sql){
  PreparedStatement ps=null;
  try {
   ps=conn.prepareStatement(sql);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return ps;
 }
 
 public static void close(Connection conn){
  if(conn==null) return;
  try {
   conn.close();
   conn=null;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void close(Statement stmt){
  if(stmt==null) return;
  try {
   stmt.close();
   stmt=null;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void close(ResultSet rs){
  if(rs==null) return;
  try {
   rs.close();
   rs=null;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}


 Category的一个JavaBean:

package cn.hpu.bbs.model;

public class Category {
	private int id;
	private String name;
	private String description;
	
	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 getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
}

 

 对数据库和Category的操作类:

//说白了就是增删查修

<pre name="code" class="java">package cn.hpu.bbs.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hpu.bbs.model.Category;
import cn.hpu.bbs.util.DB;

public class CategoryService {
	public void add(Category c){
		Connection conn=DB.createConn();
		String sql="insert into category (name,description) values (?,?)";
		PreparedStatement ps=DB.prepare(conn, sql);
		try {
			ps.setString(1, c.getName());
			ps.setString(2, c.getDescription());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(ps);
		DB.close(conn);
	}
	
	public List<Category> list(){
		Connection conn=DB.createConn();
		String sql="select * from category";
		PreparedStatement ps=DB.prepare(conn, sql);
		List<Category> categories=new ArrayList<Category>();
		try {
			ResultSet rs=ps.executeQuery();
			Category c=null;
			while(rs.next()){
				c=new Category();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
				categories.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(ps);
		DB.close(conn);
		
		return categories;
	}
	
	public void delete(Category c){
		deleteById(c.getId());
	}
	
	public void deleteById(int id){
		Connection conn=DB.createConn();
		String sql="delete from category where id=?";
		PreparedStatement ps=DB.prepare(conn, sql);
		try {
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(ps);
		DB.close(conn);
	}
	
	public void update(Category c){
		Connection conn=DB.createConn();
		String sql="update category set name = ? , description = ? where id = ?";
		PreparedStatement ps=DB.prepare(conn, sql);
		try {
			ps.setString(1, c.getName());
			ps.setString(2, c.getDescription());
			ps.setInt(3, c.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(ps);
		DB.close(conn);
	}
	
	public Category loadById(int id){
		Connection conn=DB.createConn();
		String sql="select * from category where id=?";
		PreparedStatement ps=DB.prepare(conn, sql);
		Category c=null;
		try {
			ps.setInt(1, id);
			ResultSet rs=ps.executeQuery();
			if(rs.next()){
				c=new Category();
				c.setId(rs.getInt("id"));
				c.setName(rs.getString("name"));
				c.setDescription(rs.getString("description"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DB.close(ps);
		DB.close(conn);
		return c;
	}
}


 

这个数据库建立和关闭个人感觉不太规范,以前做的项目里面写的很规范,这次仅供参考学习。


现在在学SSH框架,JSP都是之前学的了。等到掌握了hibernate,数据库就不需要那么繁琐。笔者想说的是,即使有了框架,还是要老老实实的学好基础的东西,才能更加熟练的去运用框架,因为你懂得其中的机理。


转载请注明出处!程序猿之洞:http://blog.csdn.net/acmman

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光仔December

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值