package com.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.briup.JdbcFactory;
public class Jdbc_oracle_statement {
//设置连接数据库地址
private String url;
//设置oracle用户名
private String name;
//设置oracle用户名的密码
private String pass;
//设置oracle连接对象
private Connection conn=null;
//设置执行sql语句的对象
private Statement statement=null;
//定义构造函数(构造器)
public Jdbc_oracle_statement()
{
try {
//加载连接oracle数据库类
Class.forName("oracle.jdbc.driver.OracleDriver");
//通过 连接oracle地址、oracle用户名、oracle密码获得一个连接conn
conn=DriverManager.getConnection(url,name,pass);
//获得一个执行sql语句的对象
statement=conn.createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获得连接
public Connection getConnection() throws SQLException
{
//返回数据库的连接
return conn;
}
//执行sql语句 insert update delete 返回执行的行数
public int executeUpdateSQL(String sql) throws SQLException
{
//执行sql语句
int count=statement.executeUpdate(sql);
// closeAll(conn,statement);
return count;
}
//返回执行是否成功!!成功为false 失败为true
public boolean executeSQL(String sql) throws SQLException
{
boolean flag=statement.execute(sql);
return flag;
}
//执行sql语句 返回执行的结果ResultSet
public ResultSet executeQuery(String sql) throws SQLException
{
ResultSet rs=statement.executeQuery(sql);
return rs;
}
public void closeAll(Connection conn, Statement st) {
try {
if (conn != null) {
conn.close();
}
if (st != null) {
st.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
Jdbc_oracle_statement jdbc=new Jdbc_oracle_statement();
// System.out.println(jdbc.executeSQL(sql));
System.out.println(jdbc.executeUpdateSQL(sql2));
ResultSet rs=jdbc.executeQuery(sql1);
while(rs.next())
{
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
}
//
//
// System.out.println(jdbc.executeUpdateSQL(sql2));
//
//
}
}