ame,job,mgr,hiredate,sal,comm,deptno) values("
+emp.getEmpNo()
+","
+"'"
+emp.getEname()
+"',"
+"'"
+emp.getJob()
+"',"
+emp.getMgr()
+","
+"to_date('"
+emp.getHiredate()
+"','yyyy-mm-dd'),"
+emp.getSal()
+","
+emp.getComm()+","+emp.getDeptno()+")";
try {
con=ConnectionSource.getConnection();
stmt=con.createStatement();
flag =stmt.executeUpdate(sql);
//Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing,
//such as an SQL DDL statement.
//either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0
//for SQL statements that return nothing
//这个flag返回有两种情况:1.返回执行完的行数
//如果是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("释放资源发生异常!");
}
}
}
步骤五:测试插入数据是否成功
?
在EmpDAO类的main方法中,调用add方法,代码如下所示:
?
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; 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) { System.out.println("释放资源时发生异常!"); } } } public void add(Emp emp){ Connection con=null; Statement stmt=null; int flag=-1; String sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values("+emp.getEmpNo()+","+"'"+emp.getEname()+"',"+"'"+emp.getJob()+"',"+emp.getMgr()+","+"str_to_date('"+emp.getHiredate()+"','%Y-%m-%d %H:%i:%s'),"+emp.getSal()+","+emp.getComm()+","+emp.getDeptno()+")"; try { con=ConnectionSource.getConnection(); stmt=con.createStatement(); flag =stmt.executeUpdate(sql); //Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, //such as an SQL DDL statement. //either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 //for SQL statements that return nothing //这个flag返回有两种情况:1.返回执行完的行数 //如