Java开发基础-JDBC-基本使用步骤—02

/**
 * 需要:修改新添加的员工信息【上节】
 * update emp
 * set hiredate = sysdate,comm=100,job='讲师'
 * @author Cher_du
 *
 */
public class JdbcDemo02 {
	
	public static void main(String[] args) {
		//1.加载驱动
		Connection conn = null;
		
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			//2.创建连接
			/*
			 * getConnection(url,user,password)
			 * url:数据库具体连接地址
			 * user:连接数据库服务器的用户名
			 * password:连接数据库服务器的密码
			 */
			
			conn = DriverManager.getConnection(
										"jdbc:oracle:thin:@localhost:1521:orcl",
										"learn", 
										"learn");
			System.out.println(conn);
			
			String sql = "Update emp set "+//注意拼接sql后面要留有一个空格,不然后台执行sql会直接连接在一起,造成语法报错
			             "hiredate=sysdate, "+
						 "comm=100,job='讲师' "+
					     "where empno=1000 ";
			Statement stmt = conn.createStatement();
			
			int count = stmt.executeUpdate(sql);
			
			if(count >0){
				System.out.println("数据更新了!");
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("加载驱动错误!",e);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("创建连接失败!",e);
		}finally{
			if(conn !=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
		}
	}

}

执行程序后数据库表中数据变化

猜你喜欢

转载自blog.csdn.net/coder_boy_/article/details/80574495