① 连接数据库并查询数据
② 插入数据
③ 更新数据
④ 删除数据
import java.sql.*;
public class JdbcDemo {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String user = "username";
String password = "password";
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
以上代码演示了如何连接Oracle数据库,并查询其中的数据。其中url、user和password需要根据实际情况进行替换。
课程概述(课程共6100字,4个代码举例)