java如何连接数据库executeUpdate()和executeQuery()
更新时间:2022年3月23日 14:37 点击:274 作者:一身正气z
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语句里,双引号的里面套双引号,会有歧义
会报错
应该在里面放单引号
以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。
原文出处:https://blog.csdn.net/wudingan/article/details/100026367
相关文章
- 这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试...2022-02-01
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
java 运行报错has been compiled by a more recent version of the Java Runtime
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)...2021-04-01- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
- 在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了Java如何发起http请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-31
- 说起C#和Java这两门语言(语法,数据类型 等),个人以为,大概有90%以上的相似,甚至可以认为几乎一样。但是在工作中,我也发现了一些细微的差别...2020-06-25
- C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
- 通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
- 某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
- mysqldump命令的用法1、导出所有库系统命令行mysqldump -uusername -ppassword --all-databases > all.sql 2、导入所有库mysql命令行mysql>source all.sql; 3、导出某些库系统命令行mysqldump -uusername -ppassword...2015-10-21
- 这篇文章主要介绍了解决Java处理HTTP请求超时的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 1005:创建表失败1006:创建数据库失败1007:数据库已存在,创建数据库失败1008:数据库不存在,删除数据库失败1009:不能删除数据库文件导致删除数据库失败1010:不能删除数据目录导致删除数据库失败1011:删除数据库...2013-09-23
- 这篇文章主要介绍了java 判断两个时间段是否重叠的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了超简洁java实现双色球若干注随机号码生成(实例代码),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01