📄 f0f8cef194cd001d161a8fa9da235848
字号:
package com.csthit.phonebook.dao;
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 com.csthit.phonebook.Bean.Address;
import com.csthit.phonebook.db.ConnClass;
public class AddressDAO {
private static ConnClass cc= ConnClass.getInstance();
private static Connection conn =cc.getConn(); //获取单例中的Conncetion
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
//查询所有数据
public static ArrayList queryAll() {
ArrayList listall = new ArrayList();
try {
String sql = "select * from address";
pstmt =conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Address addr = new Address();
int id = rs.getInt(1);
int uid =rs.getInt(2);
String pname = rs.getString(3);
String phone = rs.getString(4);
int age = rs.getInt(5);
String email = rs.getString(6);
String address = rs.getString(7);
addr.setId(id);
addr.setUid(uid);
addr.setPname(pname);
addr.setPhone(phone);
addr.setAge(age);
addr.setEmail(email);
addr.setAddress(address);
listall.add(addr);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return listall;
}
// 插入一条数据
public static int insert(Address address) {
String sql = "insert into address values(?,?,?,?,?,?)";
int i = 0;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, address.getUid());
pstmt.setString(2, address.getPname());
pstmt.setString(3, address.getPhone());
pstmt.setInt(4, address.getAge());
pstmt.setString(5, address.getEmail());
pstmt.setString(6, address.getAddress());
i = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return i;
}
// 根据ID查询
public static Address queryById(int id) {
try {
String sql = "select * from address where id=?";
pstmt =conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
Address address = new Address();
address.setId(id);
address.setUid(1);
address.setPname(rs.getString("pname"));
address.setPhone(rs.getString("phone"));
address.setAge(rs.getInt("age"));
address.setEmail(rs.getString("email"));
address.setAddress(rs.getString("address"));
return address;
} else {
return null;
}
} catch (SQLException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 根据ID修改
public static int update(Address address) {
int i = 0;
try {
String sql = "update address set pname=?,phone=?,age=?,email=?,address=? where id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, address.getPname());
pstmt.setString(2, address.getPhone());
pstmt.setInt(3, address.getAge());
pstmt.setString(4, address.getEmail());
pstmt.setString(5, address.getAddress());
pstmt.setInt(6, address.getId());
i = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
return 0;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return i;
}
//根据ID删除
public static int deletebyID(int id) {
int i=0;
String sql = "delete from address where id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
i=pstmt.executeUpdate();
return i;
} catch (Exception e) {
e.printStackTrace();
return 0;
} finally {
try {
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -