在对mySql、oracle数据库进行操作的时候,我们往往会有这样的困惑:
1、新增oracle数据时,按照mySql的写法为什么新增不了?
2、多个数据表,通过外键关联,如何通过一条sql语句把所有需要的表关联起来并且获取到我所需要的数据内容?
那么在下面,将详细讲解单表的增删改成及多表的关联查询该如何书写sql语句:
首先,我们要建立数据库的连接,在src目录下面创建一个db.properties的类,里面封装的内容为:
user=数据库用户名
password=数据库密码
url=jdbc\:oracle\:thin\数据库连接地址
driver=oracle.jdbc.driver.OracleDriver
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String user;
private static String password;
static{
try {
//0读取配置文件
Properties prop = new Properties();
InputStream is = JDBCUtils.class.getResourceAsStream("/db.properties");
prop.load(is);
is.close();
driver = prop.getProperty("driver");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
//1 注册驱动
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//1 获得连接
public static Connection getConnection(){
Connection conn = null;
try {
//2 获得连接
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("创建连接失败!");
}
return conn;
}
//2 释放资源
//1> 参数可能为空
//2> 调用close方法要抛出异常,确保即使出现异常也能继续关闭
//3>关闭顺序,需要从小到大
public static void close(Connection conn , Statement st , ResultSet rs){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(st!=null){
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
System.out.println(getConnection());
}
}
一、单表的增删改查
单表的结构为:表名:tb_info 字段名:①主键:ID 自增 ②用户名:NAME ③年龄:AGE
1、新增:新增不同于mySql的新增语句,因为mySql中主键一旦设置好,那么在我们执行新增语句时,主键会自动+1,不需要我们做其他操作,如:
mySql的新增语句为:
//当调用这个方法时会会获取一个返回值,当返回值为1时,证明新增了一条数据
public int inserInto(){
//定义一个默认数值为0
int count = 0;
//1. 获得连接
Connection conn = JDBCUtils.getConnection();
String name = "老王";
String age = "18";
//2. 准备sql
String sql = " INSERT INTO tb_info (NAME,AGE) VALUES (?,?)";
//3. 获得PrepareStatement对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
//4. 设置参数
ps.setString(1,name);
ps.setString(2,age);
//5. 执行sql
count = ps.executeUpdate();
//如果result !=1,没有获取到数据,证明保存失败
return count;
} catch (SQLException e) {
e.printStackTrace();
}finally{
//6. 关闭资源
JDBCUtils.close(conn, ps, null);
}
return count;
}
public int insert() {
//定义一个默认数值为0
int count = 0;
//1. 获得连接
Connection conn = JDBCUtils.getConnection();
//2. 准备sql语句,其中ID为主键
String sql = " INSERT INTO tb_info(ID,NAME,AGE) values(序列号.nextval,?,?)";
//3. 获得PrepareStatement对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
//4. 设置参数
ps.setString(1, driverInfo.getDRIVER_NAME());
ps.setString(2, driverInfo.getDRIVER_ID());
//5. 执行sql
count = ps.executeUpdate();
//如果result !=1,没有获取到数据,证明保存失败
} catch (SQLException e) {
e.printStackTrace();
}finally{
//6. 关闭资源
JDBCUtils.close(conn, ps, null);
}
return count;
}
public int updateVehiSafe(VehiSafe vehiSafe) {
//定义一个默认数值为0
int count = 0;
//1.获得连接
Connection conn = JDBCUtils.getConnection();
String sql = "update tb_vehicle_extrude set VEHI_CUSTOMERS_NUM=?," +
"vehi_card_date=to_date('"+vehiSafe.getVEHI_CARD_DATE().toString()+"','yyyy-MM-dd') where vehi_no=?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
//4. 设置参数
ps.setString(1, vehiSafe.getVEHI_CUSTOMERS_NUM());
ps.setString(2, vehiSafe.getVEHI_NO());
//5. 执行sql,修改成功返回1
count = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
//6. 关闭资源
JDBCUtils.close(conn, ps, null);
}
return count;
}
public class Department {
private String OWNER_NAME;
private String BA_NAME;
private String DEPARTMENT_NAME;
public String getOWNER_NAME() {
return OWNER_NAME;
}
public void setOWNER_NAME(String oWNER_NAME) {
OWNER_NAME = oWNER_NAME;
}
public String getBA_NAME() {
return BA_NAME;
}
public void setBA_NAME(String bA_NAME) {
BA_NAME = bA_NAME;
}
public String getDEPARTMENT_NAME() {
return DEPARTMENT_NAME;
}
public void setDEPARTMENT_NAME(String dEPARTMENT_NAME) {
DEPARTMENT_NAME = dEPARTMENT_NAME;
}
}