maven依赖
<dependency>
<groupId>com.jslsolucoes</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
public class oracle {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
@Test
public void jdbc(){
try {
//1.注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.获取数据库连接
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@//localhost:1521/ORCL", "scott", "123456"
);
//3.创建Statement
String sql="select * from books";
preparedStatement = connection.prepareStatement(sql);
//4.执行查询语句
resultSet=preparedStatement.executeQuery();
//5.处理结果
while (resultSet.next()){
System.out.println(resultSet.getString("uname"));
}
//JDBC修改
sql="update books set uname=? where uuid=1";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,"Java2");
int i = preparedStatement.executeUpdate();
System.out.println("i="+i);
} catch (Exception e) {
e.printStackTrace();
}finally {
//6.关闭
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
结果:
Java
C++
Python
i=1
来源:CSDN
作者:陌_凡
链接:https://blog.csdn.net/qq_36957411/article/details/104736389