1. 程式人生 > >JDBC程序執行步驟--repeat

JDBC程序執行步驟--repeat

rman 取數 manage 預處理 處理 .sql bsp jar包 dep

package com.lucia.repeat;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
* 主要是練習jdbc程序的運行步驟
* @author lenovo
*
*/
public class JDBCRepeat {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
//加載數據庫驅動 connection prepareStatement resultSet
Class.forName("oracle.jdbc.driver.OracleDriver");
//通過驅動管理類來獲取數據庫連接
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","bos","bos");
//定義sql語句
String sql = "select * from t_user where c_id = ?";
//預處理
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setInt(1, 10003);
//向數據庫發出sql
resultSet = preparedStatement.executeQuery();
//輸出結果
while(resultSet.next()){
System.out.println(resultSet.getString("c_username"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
//釋放資源 一定要記得
if(resultSet != null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

}

-------------

jar包:

<dependencies>
<!-- oracle數據庫驅動,需要手動安裝 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
</dependencies>

JDBC程序執行步驟--repeat