这是我以前写的代码,希望对你有所帮助,这是最基础的,锋颂扰以后学习hibernate框架的时候,代码就更加简单了,你也可以去学习下
/**
* 数据库
* **/
public class BaseDao {
public Connection connection() {
String forname = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:uework";
String user = "epwork";
String pwd = "passowrd";
Connection conn = null;
try {
Class.forName(forname);
} catch (Exception e) {
System.err.print(e.getMessage());
}
try {
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
System.err.print(e.getMessage());
}
return conn;
}
/**
*这里樱庆是关闭
**/
public void closeAll(Connection conn, ResultSet rs, PreparedStatement stmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
}
}
/**
* 添银旦加
* **/
public int addPingLun(pingluninfo pingluninfo) {
PreparedStatement stmt = null;
int rows = 0;
Connection conn = connection();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into pingluninfo(Msgid,discussUserid,Msg,Msgtime,Rbid)values(seq_n.nextval,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pingluninfo.getDiscussUserid());
stmt.setString(2, pingluninfo.getMsg());
stmt.setString(3, sdf.format(pingluninfo.getMsgtime()));
stmt.setInt(4, pingluninfo.getRbid());
rows = stmt.executeUpdate();
if (rows <= 0) {
return -1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
/**
* 查询
* **/
public List
网上有好多的呀 ,你可以去找找 ,很多的java书上也有的