3个回答
展开全部
一个完整的数据库2000的连接,加增删改查的方法实现,
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import Bean.Bean;
public class shixian implements DAO {
public void delete(Bean bean) throws Exception {
// 删除数据
String sql = "delete from person where name=?";
Conn conn = new Conn();
PreparedStatement pre;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.executeUpdate();
pre.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void insert(Bean bean) throws Exception {
// 增加数据
String sql = "insert into person values(?,?)";
Conn conn = new Conn();
PreparedStatement pre = null;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(2, bean.getPassword());
pre.executeUpdate();
pre.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Bean select(String name) throws Exception {
// 查询数据
String sql = "select name,password from person where name=?";
Conn conn=new Conn();
Bean bean = new Bean();
PreparedStatement pre = null;
try{
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, name);
ResultSet rs = pre.executeQuery();
if (rs.next()) {
bean.setName(rs.getString(1));
bean.setPassword(rs.getString(2));
}
rs.close();
pre.close();
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
public void update(Bean bean) throws Exception {
// 修改数据
String sql = "insert into person values(?,?)";
Conn conn = new Conn();
PreparedStatement pre = null;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(1, bean.getPassword());
pre.executeUpdate();
pre.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import Bean.Bean;
public class shixian implements DAO {
public void delete(Bean bean) throws Exception {
// 删除数据
String sql = "delete from person where name=?";
Conn conn = new Conn();
PreparedStatement pre;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.executeUpdate();
pre.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void insert(Bean bean) throws Exception {
// 增加数据
String sql = "insert into person values(?,?)";
Conn conn = new Conn();
PreparedStatement pre = null;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(2, bean.getPassword());
pre.executeUpdate();
pre.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Bean select(String name) throws Exception {
// 查询数据
String sql = "select name,password from person where name=?";
Conn conn=new Conn();
Bean bean = new Bean();
PreparedStatement pre = null;
try{
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, name);
ResultSet rs = pre.executeQuery();
if (rs.next()) {
bean.setName(rs.getString(1));
bean.setPassword(rs.getString(2));
}
rs.close();
pre.close();
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
public void update(Bean bean) throws Exception {
// 修改数据
String sql = "insert into person values(?,?)";
Conn conn = new Conn();
PreparedStatement pre = null;
try {
pre = conn.getConn().prepareStatement(sql);
pre.setString(1, bean.getName());
pre.setString(1, bean.getPassword());
pre.executeUpdate();
pre.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看看jdbc
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询