Java常用的API:JDBC
JDBC(Java Database Connectivity)是Java中用于操作数据库的API。使用JDBC,我们可以通过Java程序读取和写入数据库中的数据。以下是介绍Java常用的JDBC API。
连接数据库
在JDBC中,首先要做的是连接到数据库。通常我们需要提供数据库的URL、用户名和密码。以下是一个连接Oracle数据库的示例代码:
📎import java.sql.*;
public class ConnectionExample {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
conn = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connected to database");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
这个示例使用Oracle数据库,首先要加载Oracle JDBC驱动程序的类,在url中提供数据库的地址,用户名和密码。然后使用DriverManager类获取数据库连接。最后,我们在finally块中关闭数据库连接。
执行SQL语句
当连接到数据库后,我们可以使用Statement或PreparedStatement来执行SQL语句。Statement常常用于静态SQL语句,PreparedStatement常常用于动态SQL语句。以下是一个使用PreparedStatement执行插入操作的示例代码:
📎import java.sql.*;
public class PreparedStatementExample {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
conn = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connected to database");
String sql = "INSERT INTO products (product_id, product_name, price) " +
"VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 101);
pstmt.setString(2, "Apple");
pstmt.setDouble(3, 5.99);
pstmt.executeUpdate();
pstmt.setInt(1, 102);
pstmt.setString(2, "Orange");
pstmt.setDouble(3, 3.99);
pstmt.executeUpdate();
System.out.println("Records inserted successfully");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
这个示例使用prepareStatement方法创建一个准备好的语句。然后使用set方法在预处理语句中设置参数,并使用executeUpdate方法执行插入操作。
查询
在JDBC中,要查询数据库,我们可以使用Statement或PreparedStatement来执行SELECT语句。以下是一个执行查询操作的示例代码:
📎import java.sql.*;
public class SelectExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "scott";
String password = "tiger";
conn = DriverManager.getConnection(dbURL, username, password);
System.out.println("Connected to database");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM products");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" +
rs.getString(2) + "\t" +
rs.getDouble(3));
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
这个示例首先创建Statement对象然后使用executeQuery方法执行查询操作。在while循环中,我们使用ResultSet对象来访问查询结果。
这篇文章介绍了JDBC API的一些基础。连接数据库、执行SQL语句和查询是JDBC编程中必需的基本操作。JDBC API提供了许多不同的方式来实现这些操作,选择合适的方式,可以提高程序的性能并减少错误的发生。