导入jar包【ojdbc+数值】
在oracle安装目录下搜索ojdbc即可,随便选取其中一个即可
对添加的相关代码做了一个精简
package TEST;
import java.sql.*;
public class TestConnection {
static String url = "jdbc:oracle:thin:@localhost:1521:xxxx";
static String user = "xxxx";
static String password = "xxxx";
private static Connection connection;
private static Statement statement;
private static PreparedStatement preparedStatement;
public static void add(){
try {
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//获取connection
connection = DriverManager.getConnection(url,user,password);
//添加,方式一(存在SQL注入)
statement = connection.createStatement();
statement.executeUpdate("insert into test_a(id,name) values ('id53','name53')");
//添加,方式二(防止SQL注入)
preparedStatement = connection.prepareStatement("insert into test_a(id,name) values (?,?)");
preparedStatement.setString(1,"id54");
preparedStatement.setString(2,"name54");
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
try {
//关闭preparedStatement
if(preparedStatement != null){
preparedStatement.close();
}
//关闭statement
if (statement != null){
statement.close();
}
//关闭connection
if (connection != null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//测试
public static void main(String[] args){
add();
}
}
对oracle数据库进行增删改查(未精简)
import java.sql.*;
public class TestConn {
static Connection connection;
static Statement statement;
static ResultSet resultSet;
static PreparedStatement preparedStatement;
//初始化
public static void initialize(){
String url = "jdbc:oracle:thin:@localhost:1521:xxxx";
String user = "xxxx";
String password = "xxxx";
//1.加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
//2.获取Connection
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//添加
public static void add(){
initialize();
String addSql = "insert into test_a(id,name) values('2','3')";
try {
statement = connection.createStatement();
int i = statement.executeUpdate(addSql);
if (i<1){
System.out.println("添加失败!");
}else{
System.out.println("添加成功!");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
closeStatement(statement);
closeConnection(connection);
}
}
//删除和修改
public static void delOrUpd(){
initialize();
//String Sql = "delete test_a where id = '2'";
String sql = "update test_a set name = 'xxc' where id = '1'";
try {
statement = connection.createStatement();
int i = statement.executeUpdate(sql);
if (i<1){
System.out.println("失败!");
}else {
System.out.println("成功!");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
closeStatement(statement);
closeConnection(connection);
}
}
//查询(SQL注入风险)
public static void query(){
initialize();
String querySql = "select ID,NAME from TEST_A t";
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(querySql);
while (resultSet.next()){
String id = resultSet.getString("id");
String name = resultSet.getString("name");
System.out.println("id:"+id+" name:"+name);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
closeResultSet(resultSet);
closeStatement(statement);
closeConnection(connection);
}
}
//查询(无SQL注入)
public static void query2(){
initialize();
String query2Sql = "select ID,NAME from TEST_A t where ID = ?";
try {
preparedStatement = connection.prepareStatement(query2Sql);
preparedStatement.setString(1,"id1");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String id = resultSet.getString("id");
String name = resultSet.getString("name");
System.out.println("id:"+id+" name:"+name);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
closeResultSet(resultSet);
closePreparedStatement(preparedStatement);
closeConnection(connection);
}
}
//关闭connection
public static void closeConnection(Connection connection){
if (connection != null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//关闭statement
public static void closeStatement(Statement statement){
if (statement != null){
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//关闭preparedStatement
public static void closePreparedStatement(PreparedStatement preparedStatement){
if (preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//关闭ResultSet
public static void closeResultSet(ResultSet resultSet){
if (resultSet !=null){
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//执行测试
public static void main(String[] args){
//只测试query2
query2();
}
}