JDBC代碼

配置文件java

在javaResource下創建一個之後綴爲.properties的File文件sql

jdbc.driver_class=oracle.jdbc.driver.OracleDriver
jdbc.connection.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.connection.username=user1
jdbc.connection.password=user1

寫建立數據訪問的工具類數據庫

 
 

package dao;
import java.sql.*;oracle

 
 

import util.ConfigManager;工具

 
 


public class NewsDao {
//查詢新聞信息
public void getNewslist(){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String driver=ConfigManager.getInstance().getString("jdbc.driver_class");
String url=ConfigManager.getInstance().getString("jdbc.connection.url");
String username=ConfigManager.getInstance().getString("jdbc.connection.username");
String password=ConfigManager.getInstance().getString("jdbc.connection.password");
try {
//Class.forname()啓動驅動
Class.forName(driver);
//DriveManager鏈接數據庫
con=DriverManager.getConnection(url,username,password);
//Statement對象,執行SQL語句
stmt=con.createStatement();
//處理執行結果
String sql="select * from news_detail";
rs=stmt.executeQuery(sql);
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");
System.out.println(id+"\t"+title+"\t"+summary+"\t"+
content+"\t"+author+"\t"+time);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void add(int id,int categoryId,String title,String summary,String content,Date createdate){
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String driver=ConfigManager.getInstance().getString("jdbc.driver_class");
String url=ConfigManager.getInstance().getString("jdbc.connection.url");
String username=ConfigManager.getInstance().getString("jdbc.connection.username");
String password=ConfigManager.getInstance().getString("jdbc.connection.password");
try {
//Class.forname()啓動驅動
Class.forName(driver);
//DriveManager鏈接數據庫
con=DriverManager.getConnection(url,username,password);
//Statement對象,執行SQL語句
String sql="insert into news_detail(id,categoryId,title,summary,content,createdate) values(?,?,?,?,?,?)";
pstmt=con.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.setInt(2, categoryId);
pstmt.setString(3, title);
pstmt.setString(4, summary);
pstmt.setString(5, content);
pstmt.setTimestamp(6, new Timestamp(createdate.getTime()));
//處理執行結果
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("插入成功");
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

} try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void modify(int id,String title){
Connection con=null;
PreparedStatement pstmt=null;
String driver=ConfigManager.getInstance().getString("jdbc.driver_class");
String url=ConfigManager.getInstance().getString("jdbc.connection.url");
String username=ConfigManager.getInstance().getString("jdbc.connection.username");
String password=ConfigManager.getInstance().getString("jdbc.connection.password");
try {
//Class.forname()啓動驅動
Class.forName(driver);
//DriveManager鏈接數據庫
con=DriverManager.getConnection(url,username,password);
//Statement對象,執行SQL語句
String sql="update news_detail set title=? where id=?";
pstmt=con.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setInt(2, id);
//處理執行結果
int i=pstmt.executeUpdate();
if(i>0){
System.out.println("修改爲功");
}


} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

} try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}測試

//測試url

public static void main(String[] args){
NewsDao newsdao=new NewsDao();
//newsdao.add(3, 3, "a達到法定", "違法的法國德國", "餓死的發生過個", new Date(0));
//newsdao.modify(2,"我要去睡覺");
//newsdao.getNewslist();
newsdao.change(2, "goodgoodstudaydaydayup");
}
}        spa

相關文章
相關標籤/搜索