最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java如何連接數(shù)據(jù)庫executeUpdate()和executeQuery()

 更新時間:2022年03月23日 11:35:34   作者:一身正氣z  
這篇文章主要介紹了java如何連接數(shù)據(jù)庫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

//沒有返回值,參數(shù)是個字符串部門名稱就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來接收數(shù)據(jù)庫的信息
				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 {
			//創(chuàng)建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.字符串的拼接必須在雙引號的基礎上被單引號套住

上面有個小陷阱

如果加了

會正常執(zhí)行,如果沒有加,會因為字段不是字符串而報錯.

結果集為空

2.在Bean類,默認的構造方法還與參數(shù)順序有關

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

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

測試main方法里,插入的數(shù)據(jù)的類型順序決定了調用哪個構造方法.

3.構造方法的方法名就是類名....

4.system.out.println 里打印加不加toString的區(qū)別

看起來沒有區(qū)別(這個不敢肯定)

5.sql語句里,雙引號的里面套雙引號,會有歧義

會報錯

應該在里面放單引號

execute()和executeUpdate()主要區(qū)別

  • execute()返回一個boolean類型值,true表示第一個結果是ResultSet對象,false表示第一個結果是沒有結果的更新語句(insert,delete,update)。
  • executeUpdate()返回一個int類型值,表示有幾條數(shù)據(jù)受到了影響。

此外,execute()還可以通過getResultSet()獲得執(zhí)行語句后的結果;

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關文章

  • Java代碼中如何去掉煩人的“!=null”

    Java代碼中如何去掉煩人的“!=null”

    這篇文章主要介紹了Java代碼中去掉煩人的“!=null”,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • Springboot Activemq整合過程代碼圖解

    Springboot Activemq整合過程代碼圖解

    這篇文章主要介紹了Springboot Activemq整合過程代碼圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 一文讀懂Spring Bean的生命周期

    一文讀懂Spring Bean的生命周期

    今天我們來說一說 Spring Bean 的生命周期,小伙伴們應該在面試中經常遇到,這是正?,F(xiàn)象,本文讓更多的小伙伴們可以輕松的讀懂 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中優(yōu)雅處理多表數(shù)據(jù)清洗的實現(xiàn)步驟

    在SpringBoot+MyBatis中優(yōu)雅處理多表數(shù)據(jù)清洗的實現(xiàn)步驟

    數(shù)據(jù)清洗是指對數(shù)據(jù)進行處理和糾錯,以去除或修復數(shù)據(jù)集中存在的錯誤、不致、不完整和冗余的數(shù)據(jù),從而使數(shù)據(jù)更加準確、可靠和有用,本文給大家介紹了在SpringBoot和MyBatis中優(yōu)雅處理多表數(shù)據(jù)清洗的實現(xiàn)步驟,需要的朋友可以參考下
    2025-03-03
  • Windows?10卸載JDK1.8超詳細圖文教程

    Windows?10卸載JDK1.8超詳細圖文教程

    這篇文章主要介紹了Windows?10卸載JDK1.8超詳細圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟

    SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟

    這篇文章主要介紹了SpringBoot實現(xiàn)OneDrive文件上傳的詳細步驟,文中通過代碼示例和圖文講解的非常詳細,對大家實現(xiàn)OneDrive文件上傳有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • java如何根據(jù)IP獲取當前區(qū)域天氣信息詳解

    java如何根據(jù)IP獲取當前區(qū)域天氣信息詳解

    根據(jù)IP自動獲取當?shù)氐奶鞖忸A報信息這個功能大家應該都遇到過,天氣預報信息用途非常廣泛,篇文章主要給大家介紹了關于java如何根據(jù)IP獲取當前區(qū)域天氣信息的相關資料,需要的朋友可以參考下
    2021-08-08

最新評論

宁国市| 延庆县| 固镇县| 仁怀市| 兰州市| 应用必备| 东乌珠穆沁旗| 乌苏市| 新巴尔虎右旗| 晋州市| 民乐县| 高台县| 囊谦县| 延吉市| 库车县| 永德县| 沈阳市| 宕昌县| 醴陵市| 汽车| 原阳县| 新野县| 舞阳县| 绥滨县| 长垣县| 高陵县| 绍兴县| 成都市| 马公市| 大渡口区| 澳门| 宜阳县| 侯马市| 奉贤区| 黑龙江省| 南雄市| 鲁山县| 新昌县| 启东市| 舒兰市| 固原市|