标签:prepare nec exce oid 驱动 数据库连接 drive .exe 数据库
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>
标签:prepare nec exce oid 驱动 数据库连接 drive .exe 数据库
原文地址:http://www.cnblogs.com/lucia557/p/7468045.html