JDBC与数据库连接代码详解
Java实现与数据库的交互:
package com.tangjiang.crm;
import java.io.Serializable;
/**
* @author TangJiang 2017年10月30日 下午8:12:49
*
*/
public class Customer implements Serializable {
/** 客户编号 */
private long cId;
/** 客户姓名 */
private String cName;
/** 客户电话 */
private String cPhone;
/** 客户地址 */
private String cAddress;
public Customer() {
super();
}
public Customer(long cId, String cName, String cPhone, String cAddress) {
super();
this.cId = cId;
this.cName = cName;
this.cPhone = cPhone;
this.cAddress = cAddress;
}
public long getcId() {
return cId;
}
public void setcId(long cId) {
this.cId = cId;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public String getcPhone() {
return cPhone;
}
public void setcPhone(String cPhone) {
this.cPhone = cPhone;
}
public String getcAddress() {
return cAddress;
}
public void setcAddress(String cAddress) {
this.cAddress = cAddress;
}
}
package com.tangjiang.crm;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/**
* 数据库操作类
* @author TangJiang 2017年10月30日 下午8:35:51
*
*/
public class CustomerDao {
/**
* 将对象保存到数据库中
*
* @param 要保存到数据库的对象
* @return
*/
public void save(Customer c) {
String sql = "insert into customers values(customer_sq.nextval,?,?,?)";
// 获得数据库连接
Connection conn = DbUtil.getCon();
// 预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 设置参数的值
ps.setString(1, c.getcName());
ps.setString(2, c.getcPhone());
ps.setString(3, c.getcAddress());
// 执行sql
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据id删除客户信息
*
* @param
* @return
*/
public void remove(long id) {
String sql = "delete from customers where id=?";
// 获得数据库连接
Connection conn = DbUtil.getCon();
// 获得预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 设置参数值
ps.setLong(1, id);
// 执行sql语句
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据对象更新数据库
*
* @param 对象
* @return
*/
public void update(Customer c) {
String sql = "update customers set name=?,phone=?,address=? where id=?";
// 获得数据库连接
Connection conn = DbUtil.getCon();
// 预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
// 设置参数值
ps.setString(1, c.getcName());
ps.setString(2, c.getcPhone());
ps.setString(3, c.getcAddress());
ps.setLong(4, c.getcId());
// 执行sql
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询所有的数据
*
* @param
* @return 返回数据列表
*/
public List<Customer> list() {
// 设置集合存储结果集数据
LinkedList<Customer> list = new LinkedList<Customer>();
String sql = "select *from customers";
Connection conn = DbUtil.getCon();
try {
// 预编译sql语句
PreparedStatement ps = conn.prepareStatement(sql);
// 执行获得结果集
ResultSet rs = ps.executeQuery();
// 将结果集转换成LinkedList
while (rs.next()) {
Customer c = new Customer();
c.setcId(rs.getInt("id"));
c.setcName(rs.getString("name"));
c.setcPhone(rs.getString("phone"));
c.setcAddress(rs.getString("address"));
list.add(c);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
package com.tangjiang.crm;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 数据库工具类,使用懒汉单例模式,
* @author TangJiang
* 2017年10月30日 下午8:18:54
*
*/
public class DbUtil {
//定义一个Properties对象
static Properties p=new Properties();
//定义存放获取到的数据属性
private static String driver;
private static String url;
private static String user;
private static String pwd;
//定义static块实现配置文件加载
static{
try {
p.load(DbUtil.class.getClassLoader()
.getResourceAsStream("com/tangjiang/crm/jdbc.properties"));
//通过配置文件对象来获得每个key对应的value
driver=p.getProperty("driver");
url=p.getProperty("url");
user=p.getProperty("user");
pwd=p.getProperty("pwd");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 数据库连接建立方法(只能有一个地址)
* @return
*/
public static Connection getCon(){
/**数据库连接对象,默认为null*/
Connection con=null;
try {
// 加载数据库驱动包
Class.forName(driver);
//获得数据库连接对象
con=DriverManager.getConnection(
url,
user,
pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据库连接成功");
return con;
}
/**
* 定义一个关闭数据库链接对象,释放资源
*/
public static void closeCon(Connection con,ResultSet rs,Statement stm,PreparedStatement ps){
try {
//实现对象的关闭
if(rs!=null){
rs.close();
}
if(stm!=null){
stm.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.tangjiang.crm;
import java.util.List;
public class Test {
public static void main(String[] args) {
CustomerDao cd = new CustomerDao();
List<Customer>list=cd.list();
for(Customer c:list){
System.out.println(c.getcName());
}
}
}
配置文件代码就没粘贴上了!!!