码迷,mamicode.com
首页 > 数据库 > 详细

JDBC代码

时间:2015-01-29 17:23:06      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

配置文件

在javaResource下建立一个以后缀为.properties的File文件

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.*;


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();
}

}

//测试

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");
}
}        

JDBC代码

标签:

原文地址:http://www.cnblogs.com/lgxstudy/p/4260384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!