欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年03月23日 11:35:34   作者:一身正氣z  
這篇文章主要介紹了java如何連接數(shù)據(jù)庫executeUpdate()和executeQuery(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

executeUpdate

Update

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

Insert

//沒有返回值,參數(shù)是個(gè)字符串部門名稱就ok了,因?yàn)閕d的話是自增
	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();
		}
	}
 //因?yàn)閑mployeeid自增,所以不用設(shè)置
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());			
			//注冊(cè)成功后,默認(rèn)為正在審核,status為0
			pstmt.setString(7,"0");
			//注冊(cè)時(shí),默認(rèn)為員工角色,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();
		// 新建一個(gè)集合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()) {
				// 新建一個(gè)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;
				//只查詢已注冊(cè)且未審批 且 角色是員工的
				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{
				//最后總要關(guān)閉連接
				ConnectionFactory.closeConnection();
			}
			 return employeeslist;
		 } 
 
public Employee selectByNamePwd(String username, String pwd) {
		Employee employee = null;
		try {
			//創(chuàng)建PreparedStatement對(duì)象
			PreparedStatement st = null;
			//查詢語句
			String sql = "select * from employee where username='" + username + "' and  password='" + pwd + "'";
			st = conn.prepareStatement(sql);
			ResultSet rs = st.executeQuery(sql);
			//判斷結(jié)果集有無記錄,如果有:則把內(nèi)容取出來,變成一個(gè)employee對(duì)象,并且返回它
			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;
	 }

需要注意的點(diǎn)

1.字符串的拼接必須在雙引號(hào)的基礎(chǔ)上被單引號(hào)套住

上面有個(gè)小陷阱

如果加了

會(huì)正常執(zhí)行,如果沒有加,會(huì)因?yàn)樽侄尾皇亲址鴪?bào)錯(cuò).

結(jié)果集為空

2.在Bean類,默認(rèn)的構(gòu)造方法還與參數(shù)順序有關(guān)

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

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

測(cè)試main方法里,插入的數(shù)據(jù)的類型順序決定了調(diào)用哪個(gè)構(gòu)造方法.

3.構(gòu)造方法的方法名就是類名....

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

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

5.sql語句里,雙引號(hào)的里面套雙引號(hào),會(huì)有歧義

會(huì)報(bào)錯(cuò)

應(yīng)該在里面放單引號(hào)

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

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

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • springboot:接收date類型的參數(shù)方式

    springboot:接收date類型的參數(shù)方式

    這篇文章主要介紹了springboot:接收date類型的參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot2.3集成ELK7.1.0的示例代碼

    SpringBoot2.3集成ELK7.1.0的示例代碼

    這篇文章主要介紹了SpringBoot2.3集成ELK7.1.0的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • JAVA調(diào)用JavaScript方法代碼示例

    JAVA調(diào)用JavaScript方法代碼示例

    之前在一次機(jī)緣巧合的情況下,需要時(shí)用JAVA執(zhí)行js方法,查閱了一些文檔,找到了相關(guān)解決方法,這里和大家分享一下,這篇文章主要給大家介紹了關(guān)于JAVA調(diào)用JavaScript方法的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • 一文了解Seata的實(shí)現(xiàn)原理

    一文了解Seata的實(shí)現(xiàn)原理

    隨著業(yè)務(wù)發(fā)展,單體系統(tǒng)逐漸無法滿足業(yè)務(wù)的需求,分布式架構(gòu)逐漸成為大型互聯(lián)網(wǎng)平臺(tái)首選。伴隨而來的問題是,本地事務(wù)方案已經(jīng)無法滿足,分布式事務(wù)相關(guān)規(guī)范和框架應(yīng)運(yùn)而生。本文主要介紹Seata的實(shí)現(xiàn)原理
    2021-06-06
  • java中如何執(zhí)行xshell命令

    java中如何執(zhí)行xshell命令

    這篇文章主要介紹了java中如何執(zhí)行xshell命令,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java實(shí)現(xiàn)矩陣順時(shí)針旋轉(zhuǎn)90度的示例

    Java實(shí)現(xiàn)矩陣順時(shí)針旋轉(zhuǎn)90度的示例

    今天小編就為大家分享一篇Java實(shí)現(xiàn)矩陣順時(shí)針旋轉(zhuǎn)90度的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • java模仿windows計(jì)算器示例

    java模仿windows計(jì)算器示例

    這篇文章主要介紹了java模仿windows計(jì)算器示例,需要的朋友可以參考下
    2014-05-05
  • springboot2?使用activiti6?idea插件的過程詳解

    springboot2?使用activiti6?idea插件的過程詳解

    這篇文章主要介紹了springboot2?使用activiti6?idea插件,本文通過截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 理解Spring中的依賴注入和控制反轉(zhuǎn)

    理解Spring中的依賴注入和控制反轉(zhuǎn)

    這篇文章主要介紹了理解Spring中的依賴注入和控制反轉(zhuǎn),具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法

    idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法

    這篇文章主要介紹了idea中Java實(shí)體類怎樣生成序列化的版本號(hào)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論