Java语言myeclipse与数据库的链接-删除-修改等操作

6 篇文章 0 订阅
4 篇文章 0 订阅

MySQL与Myeclipse数据库的链接操作:
添加修改等一系列操作:
public List queryuser(String name,String pwd){
JdbcUtil jc=new JdbcUtil();
List list=new ArrayList();
Connection conn=jc.getConn();
PreparedStatement ps=null;
ResultSet rs=null;
String sql=“SELECT * FROM "
+ “userinfo where userName=’”+name+”’ and userPassword=’"+pwd+"’ ";
try {
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
list.add(new UserInfo(rs.getInt(“userid”),rs.getString(“username”),rs.getString(“userid”)));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jc.closeSql(rs, ps, conn);
}
return list;
}

public List<UserInfo> queryAll(){
	List<UserInfo> list=new ArrayList<UserInfo>();
	JdbcUtil jc=new JdbcUtil();
	Connection conn=jc.getConn();
	PreparedStatement ps=null;
	ResultSet rs=null;
	String sql="select * from userinfo";
	try {
		ps=conn.prepareCall(sql);
		rs=ps.executeQuery();
		while(rs.next()){
			list.add(new UserInfo(rs.getInt("userId"),
					rs.getString("username"),
					rs.getString("userPassword")));
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		jc.closeSql(rs, ps, conn);
	}
	return list;
}

public void inertDb(UserInfo userEnt){
	
	JdbcUtil jc=new JdbcUtil();
	Connection conn=jc.getConn();
	PreparedStatement ps=null;
	String sql="insert into userinfo(userId,userName,userPassword) values(?,?,?)";
	try {
		ps=conn.prepareStatement(sql);
		ps.setInt(1, userEnt.getUserId());
		ps.setString(2, userEnt.getUserName());
		ps.setString(3, userEnt.getUserPassword());
		int i=ps.executeUpdate();
		if(i>0){
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		jc.closeSql(null, ps, conn);
	}
	
}

public void updateDb(UserInfo userEnt){
JdbcUtil jc=new JdbcUtil();
Connection conn=jc.getConn();
PreparedStatement ps=null;
String sql=“update userinfo set userName=?,userPassword=? where userId=?”;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, userEnt.getUserName());
ps.setInt(2, userEnt.getUserId());
ps.setString(3, userEnt.getUserPassword());
int i=ps.executeUpdate();
if(i>0){
System.out.println(“修改成功”);
}else {
System.out.println(“修改失败”);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
jc.closeSql(null, ps, conn);
}
}
public void deleteDb(UserInfo userEnt){
JdbcUtil jc=new JdbcUtil();
Connection conn=jc.getConn();
PreparedStatement ps=null;
String sql=“delete from userinfo where userName=?”;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, userEnt.getUserName());
int i=ps.executeUpdate();
if(i>0){
System.out.println(“删除成功”);
}else {
System.out.println(“删除失败”);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值