本文假定您对oracle数据库、java有所了解,本文的主要目的是展现如何用java实现数据库的增删改查操作,在上一个帖子中已经介绍了如何用java连接数据库。
1.增加记录
目的:向oracle数据库的scott用户的emp表中增加一条记录
注意:代码中已经有详细的注释
代码如下:
public class Add {
public static void main(String[] args) {
Connection conn=null;
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//创建连接
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123456");
//sql语句
String sql="insert into emp(empno,ename,sal)values(1014,'tom',1600)";
//创建statement对象
Statement st=conn.createStatement();
//执行sql语句
int i=st.executeUpdate(sql);//返回值是受影响的行数
if (i>0) {
System.out.println("新增成功");
}else {
System.out.println("新增失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.删除记录
目的:从oracle数据库的scott用户的emp表中根据empno删除一条记录
删除前emp表的数据如下:下面我们就把empno为104的记录删除掉
代码如下:
public class Delete {
public static void main(String[] args) {
Connection conn=null;
//1.加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.建立连接
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
//System.out.println("conn:"+conn);
//3.写sql
String sql="delete from emp where empno=104";
//String sql="update emp set sal =3000 where empno=7566";
//4.创建Statement对象
Statement st=conn.createStatement();
//执行sql语句
int i =st.executeUpdate(sql); //返回值代表的是受影响的行数
if(i>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
代码执行结束后,在数据库中再次查询emp表结果如下:
3.修改记录
目的:从oracle数据库的scott用户的emp表中根据empno修改此记录的指定字段的值
修改:我们把empno为1111的记录的sal修改为9999
代码如下:
public class Update {
public static void main(String[] args) {
Connection conn=null;
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123456");
//写sql
String sql="update emp set sal=9999 where empno=1111";
//创建statement对象
Statement st=conn.createStatement();
//执行sql语句
int i=st.executeUpdate(sql);
if (i>0) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
实验结束后,查询emp表如下:
4.查询记录
目的:查询emp表
代码如下:
public class Search {
public static void main(String[] args) {
Connection conn=null;
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.建立连接
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123456");
//3.sql
String sql="select *from emp";
//4.创建statement对象
Statement st=conn.createStatement();
//5.执行sql语句
ResultSet rs=st.executeQuery(sql);
//6.遍历输出
while(rs.next()) {//判断集合有没有下一条记录
int eno=rs.getInt("empno");//参数是字段民字,建议用大写
String name=rs.getString("ename");
Date date=rs.getDate("hiredate");
System.out.println(eno+"\t"+name+"\t"+date);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
实验结果如下图:
打完收工!!!