java怎么连接数据库executeUpdate()和executeQuery()
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章主要介绍了java怎么连接数据库executeUpdate()和executeQuery()的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java怎么连接数据库e
千家信息网最后更新 2025年11月11日java怎么连接数据库executeUpdate()和executeQuery()
这篇文章主要介绍了java怎么连接数据库executeUpdate()和executeQuery()的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇java怎么连接数据库executeUpdate()和executeQuery()文章都会有所收获,下面我们一起来看看吧。
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 ListselectAll() { conn = ConnectionFactory.getConnection(); // 新建一个集合departmentsList List departmentsList = new ArrayList (); 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 selectAllEmployee(){ conn=ConnectionFactory.getConnection(); List employeeslist=new ArrayList (); 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语句里,双引号的里面套双引号,会有歧义
会报错
应该在里面放单引号
关于"java怎么连接数据库executeUpdate()和executeQuery()"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"java怎么连接数据库executeUpdate()和executeQuery()"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
数据
方法
数据库
引号
内容
员工
字符
字符串
知识
不用
参数
对象
篇文章
类型
结果
角色
语句
部门
顺序
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
微信运动改数据库
传统数据库的中心存储了所有数据
网络安全有多重要央视网
网络安全创作背景
安复仕软件开发大连
服务器电池能用多久
网络安全法只在我国适用
sal数据库包含几个日志文件
idc服务器安全组设置
普陀区营销软件开发协议
太原软件开发哪家好
网链互联网科技有限公司
app第三方数据库
绝地求生架设服务器
网络安全再多网合一
数据库约束值为1或0
网络安全架构CSF
网站服务器要多少钱
网络技术改良案例之(十)
微商网络安全
新乡市尚映网络技术
数据库约束年龄限制语句
mysql数据库可以搜索吗
小程序获取云数据库图片
网络安全主题宣传片观后感
抽取sql数据库
提升网络安全责任意识
list存储10万数据库
网络安全协议复杂性
洛阳网络技术哪家强