本教程详细介绍了使用Java JDBC技术进行数据库操作的基础知识和实践方法,专注于如何在SQL Server 2008中执行增加、删除、修改及查询等基本数据管理任务。适合希望增强数据库交互能力的开发者参考学习。
以下是使用JDBC连接SQL Server 2008进行插入、修改、删除以及查询操作的示例代码:
1. 插入数据:
```java
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
String connectionUrl = jdbc:sqlserver://localhost;databaseName=YourDBName;
conn = DriverManager.getConnection(connectionUrl, username, password);
String sqlInsert = INSERT INTO YourTableName (Column1, Column2) VALUES (?, ?);
pstmt = conn.prepareStatement(sqlInsert);
// 设置参数
pstmt.setString(1, valueOfColumn1);
pstmt.setInt(2, valueOfColumn2);
int result = pstmt.executeUpdate();
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
if (pstmt != null) try { pstmt.close(); } catch(SQLException e){ }
if (conn != null) try { conn.close(); } catch(SQLException e){ }
}
```
2. 修改数据:
```java
Connection conn = null;
PreparedStatement pstmt = null;
try{
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
String connectionUrl = jdbc:sqlserver://localhost;databaseName=YourDBName;
conn = DriverManager.getConnection(connectionUrl, username, password);
String sqlUpdate = UPDATE YourTableName SET Column1=?, Column2=? WHERE ID=?;
pstmt = conn.prepareStatement(sqlUpdate);
// 设置参数
pstmt.setString(1, newValueOfColumn1);
pstmt.setInt(2, newValueOfColumn2);
pstmt.setInt(3, yourID);
int result = pstmt.executeUpdate();
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
if (pstmt != null) try { pstmt.close(); } catch(SQLException e){ }
if (conn != null) try { conn.close(); } catch(SQLException e){ }
}
```
3. 删除数据:
```java
Connection conn = null;
PreparedStatement pstmt = null;
try{
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
String connectionUrl = jdbc:sqlserver://localhost;databaseName=YourDBName;
conn = DriverManager.getConnection(connectionUrl, username, password);
String sqlDelete = DELETE FROM YourTableName WHERE ID=?;
pstmt = conn.prepareStatement(sqlDelete);
// 设置参数
pstmt.setInt(1, yourID);
int result = pstmt.executeUpdate();
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
if (pstmt != null) try { pstmt.close(); } catch(SQLException e){ }
if (conn != null) try { conn.close(); } catch(SQLException e){ }
}
```
4. 查询数据:
```java
Connection conn = null;
Statement stmt = null;
try{
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
String connectionUrl = jdbc:sqlserver://localhost;databaseName=YourDBName;
conn = DriverManager.getConnection(connectionUrl, username, password);
String sqlQuery = SELECT * FROM YourTableName WHERE ID=?;
PreparedStatement pstmt = conn.prepareStatement(sqlQuery);
// 设置参数
pstmt.setInt(1, yourID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(Column1: +rs.getString(Column1));
System.out.println(Column2: +rs.getInt(Column2));
}
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
if (stmt != null) try { stmt.close(); } catch(SQLException e){ }
if (conn != null) try { conn.close(); } catch(SQLException e){ }
}
```
以上代码示例展示了如何使用JDBC连接SQL Server 2008进行基本的CRUD操作。请注意,需要将上述代码中的占位符(如YourDBName, username, password, YourTableName, Column1)替换为实际值,并且根据数据库表结构调整相应的字段和参数设置。