jdbc 的增删改查询

1.加载数据库驱动程序:各个数据库都会提供JDBC的驱动程序开发包,直接把JDBC操作所需要的开发包(一般为*.jar或*.zip所需的jar包;mysql-connector-java-5.1.21-bin.jar)
直接配置到classpath路径即可。
   
2.连接数据库:根据各个数据库的不同连接的地址也不同,此连接地址将由数据库厂商提供,一般在使用JDBC连接数据库的时候
都要求用户输入数据库连接的用户名和密码,用户在取得连接之后才可以对数据库进行查询或更新的操作。

3.使用语句进行数据库操作:数据库操作分为更新和查询两种操作,除了可以使用标准的SQL语句之外,对于各个数据库也可以使

用其自己提供的各种命令。

4.关闭数据库连接:数据库操作完毕之后需要关闭连接以释放资源。

package com.pw.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Map;


import com.pw.po.User;
import com.pw.util.DbUtil;



public class Userdao {
   //	数据初始化
	private String jdbcDriver="com.mysql.jdbc.Driver";
	private String jdbcUrl="jdbc:mysql://localhost:3306/pw1";
	private String jdbcUser="root";
	private String jdbcPassword="123456";
//	添加用户
	public boolean saveUser(User user){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
//			jdbcDriver驱动加载
			Class.forName(jdbcDriver);
//			数据连接
			conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
//			sql语句控制
			String sql="insert into user(username,password,flag) values('"+user.getUsername()+"','"+user.getUsername()+"','"+user.getFlag()+"')";
//			实例化Statement对象
			st= conn.createStatement();
//			执行数据库更新的SQL语句,例如:INSERT、UPDATE、DELETE等语句,返回更新的记录数
				int row= st.executeUpdate(sql);
				
				System.out.println(row);
				if(row>0){
					System.out.println("插入成功!");
				}else{
					System.out.println("插入失败!");
				}		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(st !=null){
				try{
					st.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(conn !=null) {
				try{
					st.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			} 
		}
		
		
		return flag;
	}
//	添加用户
	public boolean saveUserDbUtil(User user){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
//			
			conn=DbUtil.getConnection();
//			sql语句控制
			String sql="insert into user(username,password,flag) values('"+user.getUsername()+"','"+user.getUsername()+"','"+user.getFlag()+"')";
//			实例化Statement对象
			st= conn.createStatement();
//			执行数据库更新的SQL语句,例如:INSERT、UPDATE、DELETE等语句,返回更新的记录数
				int row= st.executeUpdate(sql);
				
				System.out.println(row);
				if(row>0){
					System.out.println("插入成功!");
				}else{
					System.out.println("插入失败!");
				}		
		}catch(Exception e){
			e.printStackTrace();
		}
		DbUtil.closeAll(st, conn);
		
		
		return flag;
	}
//删除用户
	public boolean delUser(int id){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
			Class.forName(jdbcDriver);
			conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
			String sql="delete from user where userid='"+id+"'";
			st= conn.createStatement();
				int row= st.executeUpdate(sql);
				
				if(row>0){
					System.out.println("删除成功!");
				}else{
					System.out.println("删除失败!");
				}		
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return flag;
	}
	
	
	public boolean delUserDBUtil(int id){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
			conn=DbUtil.getConnection();
			String sql="delete from user where userid='"+id+"'";
			st= conn.createStatement();
				int row= st.executeUpdate(sql);
				
				if(row>0){
					System.out.println("删除成功!");
				}else{
					System.out.println("删除失败!");
				}		
		}catch(Exception e){
			e.printStackTrace();
		}
		DbUtil.closeAll(st, conn);
		
		return flag;
	}
//	修改用户
	public boolean updateUser(Map<String, Object> map){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
			Class.forName(jdbcDriver);
			conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
			String sql="update user set password='"+(String)map.get("password")+"' where userId="+map.get("id");
			st= conn.createStatement();
				int row= st.executeUpdate(sql);
				System.out.println(row);
					
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(st !=null){
				try{
					st.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if(conn !=null) {
				try{
					st.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			} 
		}
		
		
		return flag;
	}
	
//	修改用户
	public boolean updateUserDbUtil(Map<String, Object> map){
		boolean flag =false;
		Connection conn=null;
		Statement st=null;
		try{
			
			conn=DbUtil.getConnection();
			String sql="update user set password='"+(String)map.get("password")+"' where userId="+map.get("id");
			st= conn.createStatement();
				int row= st.executeUpdate(sql);
				System.out.println(row);
					
		}catch(Exception e){
			e.printStackTrace();
		}
		DbUtil.closeAll(st, conn);
		
		
		return flag;
	}
//	查找用户
	
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值