jdbc的使用
JDBC连接
1.注册一个驱动
1)类加载Class.forname("oracle.jdbc.driver.OracleDriver");
常用的驱动类 oracle.jdbc.driver.OracleDriver
2)实例化驱动
new OracleDriver();
DriverManager.deregisterDriver(driver);
3)定义jdbc.drivers.property
System.setProperty("jdbc.drivers","oracle.jdbc.driver.OracleDriver");
-D jdbc.drivers=oracle.jdbc.driver.OracleDriver
加载驱动类(注册驱动)
2.建立数据库连接
getConnection(String url)
getConnection(String url,java.util.properties info)
getConnection(String url,String user,String password)
3.建立一个Statement类型对象
Statement
Statement stmt=connection.createStatement();
stmt.ececute(sql);
Preparedment ps=connection.createPreparedStatement(sql);
ps.ecute();
4.执行sql语句
create table t_user{
id number primary key,
name varchar2(100) not null,
salary number
};
col name for a15;
预处理【同构】的sql语句
insert into t_user(id,name,salary) values(?,?,?);
把这个有占位符的同构sql语句,发给数据库
每次替换?的值就可以
execute(sql)
返回 boolean 执行是否成功
executeQuery
resultSet 结果集
executeUpdate 返回int类型 这次执行影响了多少条数据
DML
5.处理结果集(select语句才有这一步)
stmt.addBatch(sql2);
//将给定的sql命令添加到Statement对象的列表中
int[] results = stmt.executeBatch();
//将一批命令提交给数据库来执行
6.close(关闭资源)
插入时间:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
String dateStr = sdf.format(date);
System.out.println(dateStr);
String sql = "insert into t_cus(id,last_name,start_date) values(1,'tom1',sysdate-365)";
String sql = "insert into t_cus(id,last_name,start_date) values(1,'tom1',to_date('01-11-18','dd-MM-yy'))";
String sql = "insert into t_cus(id,last_name,start_date) values(1,'tom1','04-3月-16')";
//默认是自动提交事务
//设置为false后就不自动提交事务了
conn.setAutoCommit(false);
时间戳
long数字
package com.briup.env.client;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.driver.OracleDriver;
public class DriverTest {
public static void main(String[] args) {
//加载驱动 类加载
String driverclass="oracle.jdbc.driver.OracleDriver" ;
try {
Class.forName(driverclass);
//实例化驱动
// OracleDriver driver = new OracleDriver();
// DriverManager.deregisterDriver(driver);
//定义jdbc.drivers.property
// System.getProperty("jdbc.drivers","oracle.jdbc.driver.OracleDriver");
//获取数据库连接
String url="jdbc:oracle:thin:@127.0.0.1:1521:XE";
String user="shucheng";
String password="password";
Connection conn = DriverManager.getConnection(url,user,password);
// Properties info=new Properties();
// info.setProperty("user",user);
// info.setProperty("password",password);
//
// DriverManager.getConnection(url,info);
//建立statement对象
Statement st=conn.createStatement();
String sql="insert into t_user(id,name) values(1,'zhangsan')";
sql="select id,name from t_user";
sql="delete from t_user";
st.executeUpdate(sql);//返回处理的条数
st.executeQuery(sql);//返回处理的结果集
st.execute(sql);
//preparedStatement对象
// String sql="insert into t_user(id,name) values(?,?)";
// PreparedStatement ps = conn.prepareStatement(sql);
// ps.setInt(1, 1);
// ps.setString(2, "zhangsan");
// ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}