您好,登錄后才能下訂單哦!
這篇文章主要介紹了java怎么連接數據庫executeUpdate()和executeQuery()的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇java怎么連接數據庫executeUpdate()和executeQuery()文章都會有所收獲,下面我們一起來看看吧。
//沒有返回值 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(); } }
//沒有返回值,參數是個字符串部門名稱就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(); } }
//刪除不用返回值 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; }
上面有個小陷阱
如果加了
會正常執行,如果沒有加,會因為字段不是字符串而報錯.
結果集為空
也就是說public Employee(String user,int id, String pwd){}
和 public Employee(int id,String user,String pwd){} 是不一樣的構造方法
測試main方法里,插入的數據的類型順序決定了調用哪個構造方法.
看起來沒有區別(這個不敢肯定)
會報錯
應該在里面放單引號
關于“java怎么連接數據庫executeUpdate()和executeQuery()”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“java怎么連接數據庫executeUpdate()和executeQuery()”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。