标签:package oracle public import 数据库
JDBC 提供访问数据库的标准 就是一系列接口 定义了访问数据库的通用的方法
由各个数据库厂商提供对JDBC的实现
JDBC接口的定义
数据库厂商对jdbc的实现 jar
建立连接-发送sql-执行sql-返回结果-关闭连接
JDBC API(接口)
java.sql.Connection//封装和数据库的连接
java.sql.Statement//封装SQL语句的执行
jave.sql.ResultSet//封装DQL执行的结果
package jdbc; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import oracle.jdbc.OracleDriver; public class OracleDemo01 { public static void main(String[] args) throws Exception{ //装载JDBC的实现 //Driver driver =new OracleDriver() ; 自己写 //DriverManager.registerDriver(driver); //把类装载到内存中 静态 快中执行 上面的两条 就不用自己写了 //该类的静态块中的代码会进行Driver的注册 Class.forName("oracle.jdbc.OracleDriver");//装载JDBC的实现用这个就好了 //创建连接 //调用DriverManager 的getConnection方法 //该方法返回的是数据库厂商对Connection接口的实现类的对象(因为DriverManager已经注册了数据库厂商的Driver信息) String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); //url 用于表示数据库的连接信息(ip port 数据库名称) 不同的数据库厂商有特定的url的格式和标识 // DriverManager 会根据这个标识来选取不同的驱动信息 //如果同时注册了多个数据库厂商的实现 //user //password //System.out.println(con); //执行SQL //Connection 的createStatement() 方法用于创建Statement 实现类的对象 Statement stmt = con.createStatement(); //只能执行DQL语句 //返回值是ResultSet //把SQl语句传输给数据库执行 //获取数据库传输会的结果数据 //并把这个数据封装成ResultSet 对象 ResultSet rs = stmt.executeQuery("select empno,ename name,sal from emp"); //获取结果 while(rs.next()){//.next() 游标 向下 System.out.println(rs.getString("empno")+","+rs.getString("name")+","+rs.getString("sal")); } rs.close(); //关闭连接 } }
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class OracleDemo02 { public static void main(String[] args) throws Exception{ Class.forName("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user="scott"; String pass="xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); Statement stmt = con.createStatement(); //执行DML语句 insert delete update String sql = "insert into emp2(empno,ename,sal,deptno) values(20,‘lmdtx‘,3000,10)"; int n = stmt.executeUpdate(sql);//返回整数表示,刚才的语句影响的行数 System.out.println(n); con.close(); } }
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class OracleDemo03 { public static void main(String[] args) throws Exception{ Class.forName("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); Statement stmt = con.createStatement(); String sql = "delete from emp2 where ename=‘lmdtx‘"; int n = stmt.executeUpdate(sql); System.out.println(n); con.close(); } }
会有被注入的风险
name:a‘ or ‘b‘=‘b a‘ or ‘b‘=‘b select ename,empno,sal from emp2 where ename = ‘a‘ or ‘b‘=‘b‘
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class OracleDemo04 { public static void main(String[] args) throws Exception{ Scanner in = new Scanner(System.in); System.out.print("name:"); String name = in.nextLine(); System.out.println(name); Class.forName("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); Statement stat = con.createStatement(); String sql ="select ename,empno,sal from emp2 where ename = ‘"+name+"‘"; System.out.println(sql); ResultSet rs = stat.executeQuery(sql); while(rs.next()){ System.out.println(rs.getString(1)+","+rs.getInt(2)+","+rs.getInt(3)); } con.close(); } }
PreparedStatement 接口(用这个好)
提高效率
防止SQL Injection
1创建连接
2PreparedStatement
PreparedStatement stmt = con.prepareStatement(sql)
3 stmt.setString(1,"xxx");
stmt.setInt(1,123);
package jdbc; import java.io.BufferedReader; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class OracleDemo05 { public static void main(String[] args)throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("name:"); String name = br.readLine(); Class.forName("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user = "scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); String sql = "select empno,ename,sal from emp2 where ename=?"; PreparedStatement stmt = con.prepareStatement(sql); //将 sql 中 第一个问号的值设置为字符串 stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); while(rs.next()){ System.out.println(rs.getInt(1)+","+rs.getString(2)+","+rs.getInt(3)); } con.close(); } }
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class OracleDemo06 { public static void main(String[] args) throws Exception{ Class.forName("oracle.jdbc.OracleDriver"); String url ="jdbc:oracle:thin:@x1.zongxuan.online:1521:xx"; String user ="scott"; String pass = "xxxxx"; Connection con = DriverManager.getConnection(url,user,pass); String sql ="insert into emp2(empno,ename) values(?,?)"; PreparedStatement stmt = con.prepareStatement(sql); int n =0; for(int i =1000;i<3000;i++){ stmt.setInt(1, i+1); stmt.setString(2, "sting"+i); n = stmt.executeUpdate(); System.out.println(i+","+n); } con.close(); } }
封装连接工具类
properties
文本文件, 存放的是KEY—VALUSE
driver=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@x1.zongxuan.online:1521:xx user=scott pass=xxxxx
package jdbc; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class DBUtils { private static String driver; private static String url; private static String user; private static String pass; static{ Properties props = new Properties(); try { props.load(DBUtils.class.getClassLoader().getResourceAsStream("jdbc/db.properties")); driver= props.getProperty("driver"); url= props.getProperty("url"); user= props.getProperty("user"); pass= props.getProperty("pass"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection openConnection() throws Exception{ return DriverManager.getConnection(url,user,pass); } }
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class OracleDemo08 { public static void main(String[] args) throws Exception { Connection con = DBUtils.openConnection(); String sql = "select empno,ename,sal from emp2"; PreparedStatement stmt = con.prepareStatement(sql); // 将 sql 中 第一个问号的值设置为字符串 ResultSet rs = stmt.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1) + "," + rs.getString(2) + "," + rs.getInt(3)); } con.close(); } }
日期
package jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Date; public class Oracledemo09 { public static void main(String[] args) throws Exception{ Connection con = DBUtils.openConnection(); String sql ="insert into emp2(empno,ename,hiredate)" + "values(?,?,?)"; PreparedStatement stmt =con.prepareStatement(sql); stmt.setInt(1, 4000); stmt.setString(2,"lmdtx"); stmt.setDate(3,new Date(System.currentTimeMillis())); int n =stmt.executeUpdate(); System.out.println(n); con.close(); } }
批处理
Batch 处理 批量的插入 更新
stmt.addBatch();
把刚刚设置好的数据添加批处理缓冲
stmt
执行批处理,把缓冲的数据一次传送给数据库执行
缓存的批处理量受到哭护短JVM内存的限制要指定一个合理的批量值才好
package jdbc; import java.sql.Connection; import java.sql.PreparedStatement; public class OracleDemo10 { public static void main(String[] args) throws Exception{ Connection con = DBUtils.openConnection(); String sql = "insert into emp2(empno,ename) values(?,?)"; PreparedStatement stmt = con.prepareStatement(sql); //批量插入 for(int i =5000;i<=6000;i++){ stmt.setInt(1, i); stmt.setString(2, "M"+i); stmt.addBatch();//添加批处理 } stmt.executeBatch();//执行批处理 con.close(); } }
package jdbc; import java.sql.Connection; import java.sql.PreparedStatement; public class OracleDemo10 { public static void main(String[] args) throws Exception{ Connection con = DBUtils.openConnection(); String sql = "insert into emp100(id,name) values(?,?)"; PreparedStatement stmt = con.prepareStatement(sql); //批量插入 for(int i =1;i<=2000000;i++){ stmt.setInt(1, i); stmt.setString(2, "M"+i); stmt.addBatch();//添加批处理 if(i%20000==0){ stmt.executeBatch(); } } stmt.executeBatch();//执行批处理 con.close(); } }
JDBC的事物操作
JDBC 中所有的DML会默认提交 在每条DML语句后会默认的加入commit
本文出自 “浪漫的偷笑” 博客,请务必保留此出处http://lmdtx.blog.51cto.com/6942028/1837387
标签:package oracle public import 数据库
原文地址:http://lmdtx.blog.51cto.com/6942028/1837387