1.1 需求
对分类管理使用JDBC完成CRUD操作
JDBC:Java数据库的连接
驱动:需要导入jar包。
【JDBC开发的步骤】
1.注册驱动
2.获得连接
3.获得执行SQL语句的对象
4.释放资源
【JDBC封装】
public class jdbcUtils {
private static final String driver;
private static final String url;
private static final String user;
private static final String password;
static{
InputStream inputStream = null;
try {
inputStream = new FileInputStream("src/JDBC_Enhance_Utils/jdbc.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Properties properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
}
/*
* 注册驱动的方法
* */
public static void loadDriver(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 获得连接的方法
* */
public static Connection getConnection(){
loadDriver();
Connection connection = null;
try {
connection = (Connection) DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
/*
* 释放资源的方法
* */
public static void release(ResultSet rs ,Statement st ,Connection conn){
if (rs != null) {
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (st != null) {
try {
st.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
public static void release(Statement st,Connection conn){
if (st != null) {
try {
st.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
public class testJdbcUtils {
@Test
public void test01(){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = jdbcUtils.getConnection();
String sql = "select * from user";
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
resultSet = (ResultSet) preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt("cid")+"\t"+resultSet.getString("cname"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
jdbcUtils.release(resultSet, preparedStatement, connection);
}
}
}
1.3总结JDBC的API
【Connection】
->创建执行Sql的操作
statement — createStatement()
prepareStatement — prepareStatement(String Sql)
prepareCall:Oracle调用数据库存储的过程 — prepareCall(String Sql)
->进行事务的管理
setAutoCommit()
commit()
rollback()
【Statement】
->执行SQL语句
executeQuery(String SQl)
executeUpdate(String SQl)
execute(String SQl)
-> 执行批处理
addBatch()
clearBatch()
executeBatch()
【ResultSet】
->获得处理结果
getXXX(int idx)
getXXX(String name)
->默认情况下,next();
正常情况下,结果集只能向下。