java如何连接数据库executeUpdate()和executeQuery()

 更新时间:2022年03月23日 11:35:34   作者:一身正气z  
这篇文章主要介绍了java如何连接数据库executeUpdate()和executeQuery(),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

executeUpdate

Update

//没有返回值
public void update(int count){
conn=DBUtil.getConn();
String sql="update counter set count=?";
try {					
			PreparedStatement ps = conn.prepareStatement(sql);
			//传进去的
			ps.setInt(1,count);
			ps.executeUpdate();		
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeConn();
		}   
}

Insert

//没有返回值,参数是个字符串部门名称就ok了,因为id的话是自增
	public void insert(String departmentname) {
		conn = ConnectionFactory.getConnection();
		String sql = "insert into department (departmentname) values(?)";
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, departmentname);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection();
		}
	}
 //因为employeeid自增,所以不用设置
public void insert(Employee employee){
		  conn=ConnectionFactory.getConnection();
		  String sql="insert into employee"
				  +
					"(employeename,username,password,phone,email,departmentid,status,role)" +
					" values(?,?,?,?,?,?,?,?)";
		  try {		
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,employee.getEmployeename());
			pstmt.setString(2,employee.getUsername());
			pstmt.setString(3,employee.getPassword() );
			pstmt.setString(4,employee.getPhone() );
			pstmt.setString(5,employee.getEmail());
			pstmt.setInt(6,employee.getDepartmentid());			
			//注册成功后,默认为正在审核,status为0
			pstmt.setString(7,"0");
			//注册时,默认为员工角色,role值为2
			pstmt.setString(8,"2");
			pstmt.executeUpdate();	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ConnectionFactory.closeConnection();
		}	  
	  }

Delete

//删除不用返回值	
public void delete(int departmentid) {
		conn = ConnectionFactory.getConnection();
		String sql = "delete from department where departmentid=?;";
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, departmentid);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection();
		}
	}

select

//返回int类型
public int select(){
	int count=0;
	conn=DBUtil.getConn();
	String sql = "select * from counter";
	try{
		PreparedStatement ps = conn.PreparedStatement(sql);
		ResultSet rs =ps.excuteQuery();
		if(rs.next()){
			count=rs.getInt("visitcount");
		}
	}catch{
 
	}finally{
		DBUtil.closeConn();
	}
	return count;
}
//返回部门集合
	public List<Department> selectAll() {
		conn = ConnectionFactory.getConnection();
		// 新建一个集合departmentsList
		List<Department> departmentsList = new ArrayList<Department>();
		try {
			Statement st = null;
			String sql = "select * from department";
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			Department department;
			while (rs.next()) {
				// 新建一个department来接收数据库的信息
				department = new Department();
				department.setDepartmentid(rs.getInt("departmentid"));
				department.setDepartmentname(rs.getString("departmentname"));
				departmentsList.add(department);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection();
		}
		// 返回集合
		return departmentsList;
	} 
 
//返回员工
  public List<Employee> selectAllEmployee(){
			 conn=ConnectionFactory.getConnection();
			 List<Employee> employeeslist=new ArrayList<Employee>();
			 Employee employee=null;	
			 try {
				PreparedStatement st=null;
				//只查询已注册且未审批 且 角色是员工的
				String sql="select * from employee where role='2' and status='0'";
		 		st = conn.prepareStatement(sql);
				ResultSet rs =st.executeQuery(sql);
				while(rs.next()){
					employee=new Employee();
					employee.setEmployeeid(rs.getInt("employeeid"));
					employee.setEmployeename(rs.getString("employeename"));
					employee.setUsername(rs.getString("username"));
					employee.setPhone(rs.getString("phone"));
					employee.setEmail(rs.getString("email"));
					employee.setStatus(rs.getString("status"));
					employee.setDepartmentid(rs.getInt("departmentid"));
					employee.setPassword(rs.getString("password"));
					employee.setRole(rs.getString("role"));
					employeeslist.add(employee);
				}
			 } catch (SQLException e) {
				    e.printStackTrace();
			}finally{
				//最后总要关闭连接
				ConnectionFactory.closeConnection();
			}
			 return employeeslist;
		 } 
 
public Employee selectByNamePwd(String username, String pwd) {
		Employee employee = null;
		try {
			//创建PreparedStatement对象
			PreparedStatement st = null;
			//查询语句
			String sql = "select * from employee where username='" + username + "' and  password='" + pwd + "'";
			st = conn.prepareStatement(sql);
			ResultSet rs = st.executeQuery(sql);
			//判断结果集有无记录,如果有:则把内容取出来,变成一个employee对象,并且返回它
			if (rs.next() == true) {				
				employee = new Employee();				
				employee.setEmployeeid(rs.getInt("employeeid"));
				employee.setEmployeename(rs.getString("employeename"));
				employee.setUsername(rs.getString("username"));
				employee.setPhone(rs.getString("phone"));
				employee.setEmail(rs.getString("email"));
				employee.setStatus(rs.getString("status"));
				employee.setDepartmentid(rs.getInt("status"));
				employee.setPassword(rs.getString("password"));
				employee.setRole(rs.getString("role"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection();
		}
		return employee;
	}
 public Employee selectByUsername(String username){
		 conn=ConnectionFactory.getConnection();
		 Employee employee=null;	
		 try {
			 PreparedStatement st=null;
			String sql="select * from employee where username='"+username+"'";
	 		st = conn.prepareStatement(sql);
			ResultSet rs =st.executeQuery(sql);
			if(rs.next()==true){
				employee=new Employee();
				employee.setEmployeeid(rs.getInt("employeeid"));
				employee.setEmployeename(rs.getString("employeename"));
				employee.setUsername(rs.getString("username"));
				employee.setPhone(rs.getString("phone"));
				employee.setEmail(rs.getString("email"));
				employee.setStatus(rs.getString("status"));
				employee.setDepartmentid(rs.getInt("status"));
				employee.setPassword(rs.getString("password"));
				employee.setRole(rs.getString("role"));
			}
		 } catch (SQLException e) {
			    e.printStackTrace();
		}finally{
			ConnectionFactory.closeConnection();
		}
		 return employee;
	 }

需要注意的点

1.字符串的拼接必须在双引号的基础上被单引号套住

上面有个小陷阱

如果加了

会正常执行,如果没有加,会因为字段不是字符串而报错.

结果集为空

2.在Bean类,默认的构造方法还与参数顺序有关

也就是说public Employee(String user,int id, String pwd){}

和 public Employee(int id,String user,String pwd){}  是不一样的构造方法

测试main方法里,插入的数据的类型顺序决定了调用哪个构造方法.

3.构造方法的方法名就是类名....

4.system.out.println 里打印加不加toString的区别

看起来没有区别(这个不敢肯定)

5.sql语句里,双引号的里面套双引号,会有歧义

会报错

应该在里面放单引号

execute()和executeUpdate()主要区别

  • execute()返回一个boolean类型值,true表示第一个结果是ResultSet对象,false表示第一个结果是没有结果的更新语句(insert,delete,update)。
  • executeUpdate()返回一个int类型值,表示有几条数据受到了影响。

此外,execute()还可以通过getResultSet()获得执行语句后的结果;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • Java代码中如何去掉烦人的“!=null”

    Java代码中如何去掉烦人的“!=null”

    这篇文章主要介绍了Java代码中去掉烦人的“!=null”,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Springboot Activemq整合过程代码图解

    Springboot Activemq整合过程代码图解

    这篇文章主要介绍了Springboot Activemq整合过程代码图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • 一文读懂Spring Bean的生命周期

    一文读懂Spring Bean的生命周期

    今天我们来说一说 Spring Bean 的生命周期,小伙伴们应该在面试中经常遇到,这是正常现象,本文让更多的小伙伴们可以轻松的读懂 Spring Bean 的生命周期
    2023-03-03
  • java多线程-同步块实例讲解

    java多线程-同步块实例讲解

    本文主要介绍java多线程-同步块的知识,这里整理了相关的详细资料及简单示例代码,有兴趣的小伙伴可以参考下
    2016-09-09
  • idea使用spring Initializr 快速搭建springboot项目遇到的坑

    idea使用spring Initializr 快速搭建springboot项目遇到的坑

    这篇文章主要介绍了idea使用spring Initializr 快速搭建springboot项目遇到的坑,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Spring Security登陆流程讲解

    Spring Security登陆流程讲解

    本文主要介绍了Spring Security登陆流程讲解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • 在SpringBoot+MyBatis中优雅处理多表数据清洗的实现步骤

    在SpringBoot+MyBatis中优雅处理多表数据清洗的实现步骤

    数据清洗是指对数据进行处理和纠错,以去除或修复数据集中存在的错误、不致、不完整和冗余的数据,从而使数据更加准确、可靠和有用,本文给大家介绍了在SpringBoot和MyBatis中优雅处理多表数据清洗的实现步骤,需要的朋友可以参考下
    2025-03-03
  • Windows 10卸载JDK1.8超详细图文教程

    Windows 10卸载JDK1.8超详细图文教程

    这篇文章主要介绍了Windows 10卸载JDK1.8超详细图文教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • SpringBoot实现OneDrive文件上传的详细步骤

    SpringBoot实现OneDrive文件上传的详细步骤

    这篇文章主要介绍了SpringBoot实现OneDrive文件上传的详细步骤,文中通过代码示例和图文讲解的非常详细,对大家实现OneDrive文件上传有一定的帮助,需要的朋友可以参考下
    2024-02-02
  • java如何根据IP获取当前区域天气信息详解

    java如何根据IP获取当前区域天气信息详解

    根据IP自动获取当地的天气预报信息这个功能大家应该都遇到过,天气预报信息用途非常广泛,篇文章主要给大家介绍了关于java如何根据IP获取当前区域天气信息的相关资料,需要的朋友可以参考下
    2021-08-08

最新评论