JDBC(Java DataBase Connectivity,java数据库连接),用于执行SQL语句的JavaAPI,在之前的学习当中不止一次用到过(涵盖大多课程设计//但我使用了Mysql,装载的(Mysql的驱动包),但并未对该API的原理未深究,只是简单的使用。)作为Sun公司的套产品,在Java程序当中的使用方式十分简单 ,只要你会SQL,会Java。在不考虑效率和安全的情况下十分易用简便(JDBC-Wiki)
JDBC For Oracle:
在oracle的安装目录下会有oracle的的jdbc驱动jar包:oracleexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc.jar。在添加好Jar包驱动之后进入正题:
1、注册驱动(加载驱动)
注册驱动可以多次加载不同的驱动(一般不这样做。。)
2、获取连接/打开连接
3、创建Statement对象(prepareStatement对象可以预防外界输入带‘’)
PreparedStatement:connection.prepareStatement(String sql);
创建时就需要传递sql语句,执行的时候不需要传递sql语句
如果涉及到动态参数的传递,可以使用字符串拼接,也可以使用?占位的形式给?号传值使用的是pstmt.setType(index,value);index从1开始
提供预编译的功能,某种程度上可以避免sql注入的问题
提前做语法检查,在给?赋值的过程中要求数据类型一定要匹配,这样在某种程度上可以避免因为数据类型不匹配而发生的异常
CallableStatement:主要用来执行pl/sql的一些过程,函数等。
4、执行SQL
execute() 返回boolean 第一个返回的结果是否是ResultSet对象
executeQuery() 返回ResuSet 一个结果集
executeUpdate() 返回int 即操作所影响的行数
5、如果有ResultSet对象返回,处理ResultSet
6、释放资源
package xin.stringair; import java.sql.*; public class JDBC { public static void main(String[] args) throws Exception { //加载驱动 Class.forName("oracle.jdbc.OracleDriver"); //获取连接 String url="jdbc:oracle:thin:@127.0.0.1:1521:xe",user="xin",passwrod="root"; Connection conn = DriverManager.getConnection(url,user,passwrod); //创建Statement对象 Statement stk = conn.createStatement(); String sql = "select id,last_name,salary from s_emp"; ResultSet rtk = stk.executeQuery(sql); while(rtk.next()){ int id = rtk.getInt(1); String name = rtk.getString(2); String salary = rtk.getString(3); System.out.println(id+"\t"+name+"\t"+salary); } rtk.close(); stk.close(); conn.close(); } }
在加载驱动时也可以(可以放到执行程序前的加载过程当中):
System.setProperty("jdbc.drivers","oracle.jdbc.OracleDriver" );
Driver driver = new oracle.jdbc.OracleDriver();
在使用ojdbc5(即jdbc4.0标准下)我们无需加载驱动,直接可以使用。
在获取连接时亦可以:
Connection con = DriverManager.getConnection( "jdbc:oracle:thin:xin/root@localhost:1521:xe");
简单的增删改查:
package xin.work; import java.sql.*; import java.util.Scanner; class Student{ private int id; private String name; private int age; public Student(int id,String name,int age){ this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class PostInthe { static Connection con; static PreparedStatement tk; public static void insert(Student s){ String sql = "insert into t_student values(?,?,?)"; try{ tk = con.prepareStatement(sql); tk.setInt(1, s.getId()); tk.setString(2, s.getName()); tk.setInt(3, s.getAge()); if(!tk.execute()) System.out.println("Insert Succeed!"); tk.close(); }catch(Exception e){ e.printStackTrace(); } } public static void delete(int id){ String sql = "delete from t_student where id=?"; try{ tk = con.prepareStatement(sql); tk.setInt(1, id); if(!tk.execute()) System.out.println("Delete Succeed!"); tk.close(); }catch(Exception e){ e.printStackTrace(); } } public static void update(int id,Student s){ String sql = "update t_student set id=?,name=?,age=? where id=?"; try{ tk = con.prepareStatement(sql); tk.setInt(1, s.getId()); tk.setString(2, s.getName()); tk.setInt(3, s.getAge()); tk.setInt(4,id); if(!tk.execute()) System.out.println("Update Succeed!"); tk.close(); }catch(Exception e){ e.printStackTrace(); } } public static void select(int id){ String sql = "select * from t_student where id =?"; try{ tk = con.prepareStatement(sql); tk.setInt(1,id); ResultSet pk = tk.executeQuery(); System.out.println("id\tname\t\tage"); while(pk.next()) System.out.println(pk.getInt(1)+"\t"+pk.getString(2)+"\t"+pk.getInt(3)); pk.close(); tk.close(); }catch(Exception e){ e.printStackTrace(); } } public static void toFindAll(){ String sql = "select * from t_student"; try{ tk = con.prepareStatement(sql); ResultSet pk = tk.executeQuery(); System.out.println("id\tname\t\tage"); while(pk.next()) System.out.println(pk.getInt(1)+"\t"+pk.getString(2)+"\t"+pk.getInt(3)); pk.close(); tk.close(); }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args) throws Exception { con = DriverManager.getConnection("jdbc:oracle:thin:xin/root@localhost:1521:xe"); Scanner tp = new Scanner(System.in); int choice = -1; while(choice!=0){ System.out.println("[1]Insert Information"); System.out.println("[2]delete Information"); System.out.println("[3]upadte Information"); System.out.println("[4]select Information"); System.out.println("[5]select All Information"); System.out.println("[0]exit"); choice = tp.nextInt(); switch(choice){ case 1:{ System.out.println("input id:"); int id = tp.nextInt(); System.out.println("input name:"); String name = tp.next(); System.out.println("input age:"); int age = tp.nextInt(); insert(new Student(id,name,age)); break; } case 2:{ System.out.println("input delete id:"); int id = tp.nextInt(); delete(id); break; } case 3:{ System.out.println("input change id:"); int id = tp.nextInt(); System.out.println("input new information:"); System.out.println("input id:"); int ids = tp.nextInt(); System.out.println("input name:"); String name = tp.next(); System.out.println("input age:"); int age = tp.nextInt(); update(id,new Student(ids,name,age)); break; } case 4:{ System.out.println("input delete id:"); int id = tp.nextInt(); select(id); break; } case 5:{ toFindAll(); break; } } } con.close(); } }