方法一:【工具类】
NewDao:
String sql = "select * from t1";
DbUtils db = new DbUtils();
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
public void getNewsList() {
conn = db.getConnection();
try {
Statement stms = (Statement) conn.createStatement();
rs = stms.executeQuery(sql);
while(rs.next()){
String title = rs.getString("name");
System.out.println("title=="+title);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.close(rs, stmt, conn);
}
DbUtils:
String url = "jdbc:mysql://localhost:3306/news";
String user = "root";
String password = "3838438";
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void close(ResultSet rs,Statement stmt,Connection conn) {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭结果集对象
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭 Statement 对象
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 关闭连接对象
}
}
方法二:【利用继承与接口】
BaseDao.java
package java.util.two;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class BaseDao {
protected Connection conn;
protected PreparedStatement ps;
protected Statement stmt;
protected ResultSet rs;
//获取数据库链接
public boolean getConnection() {
return true;
}
// 增、删、改、查
public int executeUpdate(String sql,Object[] params) {
int updateRows = 0;
return updateRows;
}
// 查询
public ResultSet executeSQL(String sql,Object[] params) {
return rs;
}
//关闭资源
public boolean closeResource() {
return true;
}
}
NewsDao.java
package java.util.two;
import java.sql.Date;
public interface NewsDao {
//查询新闻信息
public void getNewsList();
//增加新闻信息
public void add(int id,int category,String title,String summary,String content,Date createdate);
//删除新闻信息
public void delete(int id);
//修改新闻标题信息
public void update(int id,String title);
}
NewsDaoImpl.java
package java.util.two;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.DbUtils;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class NewsDaoImpl extends BaseDao implements NewsDao {
String sql = "select * from t1";
DbUtils db = new DbUtils();
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;;
@Override
public void getNewsList() {
conn = db.getConnection();
try {
Statement stms = (Statement) conn.createStatement();
rs = stms.executeQuery(sql);
while(rs.next()){
String title = rs.getString("name");
System.out.println("title=="+title);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.close(rs, stmt, conn);
}
@Override
public void add(int id, int category, String title, String summary, String content, Date createdate) {
// TODO Auto-generated method stub
}
@Override
public void delete(int id) {
// TODO Auto-generated method stub
}
@Override
public void update(int id, String title) {
// TODO Auto-generated method stub
}
}