设为首页 加入收藏

TOP

使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据(三)
2015-07-24 11:31:38 来源: 作者: 【 】 浏览:5
Tags:使用 JDBC 连接 MySQL 数据库 典型 案例分析 ---- 新和 插入 员工 数据
果是DDL语句那么什么都不返回。 //DDL语句:Data Definition Language //比如:CREATE DATABASE,CREATE TABLE,ALTER TABLE ,DROP TABLE,CREATE VIEW,ALTER VIEW ,DROP VIEW 等 if(flag>0){ System.out.println("新增记录成功!"); } } catch (SQLException e) { System.out.println("数据库访问异常!"); throw new RuntimeException(e); } finally{ try { if(stmt!=null){ stmt.close(); } if(con!=null){ con.close(); } } catch (SQLException e2) { System.out.println("释放资源发生异常!"); } } } public void update(Emp emp){ Connection con=null; Statement stmt=null; int flag=-1; String sql="update emp set sal="+emp.getSal()+","+"comm="+emp.getComm()+"where empno="+emp.getEmpNo(); try { con=ConnectionSource.getConnection(); stmt=con.createStatement(); flag=stmt.executeUpdate(sql); if(flag>0){ System.out.println("更新记录成功!"); } } catch (SQLException e) { System.out.println("数据库访问异常!"); throw new RuntimeException(e); }finally{ try { if(stmt!=null){ stmt.close(); } if(con!=null){ con.close(); } } catch (SQLException e2) { System.out.println("释放资源发生异常!"); } } } }

 在执行执行,上述代码之前,数据库Emp表如下图: 
 

?

\

控制台输出如下:

\

执行之后,刷新数据库,如下图;

\

?

通过上述执行结果,会发现在数据库Emp表中添加了一条员工ID为1001的记录。

步骤六:对Emp表中的数据执行更新

在EmpDAO类中,添加update方法,该方法实现将员工ID为1001的薪资更新为4500,代码如下所示:

?

	public void update(Emp emp){
		Connection con=null;
		Statement stmt=null;
		int flag=-1;
		String sql="update emp set sal="+emp.getSal()+","+"comm="+emp.getComm()+"where empno="+emp.getEmpNo();
		try {
			con=ConnectionSource.getConnection();
			stmt=con.createStatement();
			flag=stmt.executeUpdate(sql);
			if(flag>0){
				System.out.println("更新记录成功!");
			}
		} catch (SQLException e) {
			System.out.println("数据库访问异常!");
			throw new RuntimeException(e);
		}finally{
			try {
				if(stmt!=null){
					stmt.close();
				}
				if(con!=null){
					con.close();
				}
			} catch (SQLException e2) {
				System.out.println("释放资源发生异常!");
			}
		}
	}
步骤七:测试update方法更新数据是否成功

?

代码如下所示

?

package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.management.RuntimeErrorException;


import Entity.Emp;
public class EmpDAO {
	public static void main(String [] args){
		EmpDAO dao=new EmpDAO();
		//1.select all
		dao.findAll();
		//2.insert
		Emp emp=new Emp(1001,"rose","Analyst",7901,"2014-05-01",3000.00,500.00,10);
		//System.out.println("emp.getEmpNo()"+emp.getEmpNo());
		dao.add(emp);
		//3.update
		emp.setSal(4500.00);
		dao.update(emp);
	}
	public void findAll(){
		Connection con=null;
		Statement stmt=null;
		ResultSet rs=null;
		
		try {
			con=ConnectionSource.getConnection();
			stmt=con.createStatement();
			rs=stmt.executeQuery("select empno,ename,sal,hiredate from emp;");
			while(rs.next()){
				System.out.println(rs.getInt("empno")+","+rs.getString("ename")+","+rs.getDouble("sal")+","+rs.getDate("hiredate"));
			}
		} catch (SQLException e) {
			System.out.println("数据库访问异常!");
			throw new RuntimeException(e);
		}
		finally{
			try {
				if(rs!=null){
						rs.close();
					}
					if(stmt!=null){
						stmt.close();
					}
					if(con!=null){
						con.close();
					}
			} catch (SQLException e)
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL计算字段 下一篇Mysql登陆&退出、创建&删除&选择..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)