该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package com.news.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class NewsDao {
//查询新闻信息
public void getNewsList(){
Connection connection = null;
Statement stt = null;
ResultSet rs = null;
try {
//1. Class.forName 加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2. DriverManager.getConnection(URL,用户名,密码)获得数据库连接(Connection)
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:SHOP","shop_dev_u","bdqn");
//3. 获得Statement对象,执行SQL语句
String sql = "select * from news_detail";
stt = connection.createStatement();
rs = stt.executeQuery(sql);
//4. 处理执行结果(ResultSet),释放资源
while(rs.next()){
int id = rs.getInt("id");
String title = rs.getString("title");
String summary = rs.getString("summary");
String content = rs.getString("content");
String author = rs.getString("author");
Timestamp time = rs.getTimestamp("createdate");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
stt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
NewsDao newsDao = new NewsDao();
newsDao.getNewsList();
}
}