1、封装JDBC
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月6日 上午12:08:07
* 工具类 封装JDBC
*/
package sumeng.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Administrator
*
*/
public class BeanDao {
// JDBC 驱动名及数据库 URL
private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://localhost:1433;databaseName=Green express";
// 数据库的用户名与密码
private static String name = "green";
private static String password = "12345678";
static {
try {
// 注册 JDBC 驱动
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
// 打开链接
System.out.println("连接数据库...");
return DriverManager.getConnection(url,name,password);
}
public static void closeAll(Connection conn,Statement stm,ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
if(stm!=null){
stm.close();
}
if(conn!=null){
conn.close();
}
System.out.println("关闭数据库...");
}
public int executeSQL(String sql,Object[] parma){
Connection conn = null;
PreparedStatement pstmt = null;
/* 处理SQL,执行SQL */
try {
conn = getConnection();// 得到数据库连接
pstmt = conn.prepareStatement(sql);// 得到PreparedStatement对象
if (parma != null) {
for (int i = 0; i < parma.length; i++) {
pstmt.setObject(i + 1, parma[i]); // 为预编译sql设置参数
}
}
ResultSet executeUpdate = pstmt.executeQuery();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally{
try {
BeanDao.closeAll(conn, pstmt, null);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return 0;
}
}
2、属性封装
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月6日 上午12:06:39
* 类说明
*/
package sumeng.etitly;
/**
* @author Administrator
*
*/
public class Log {
private String log_Id;
private String log_Time;
private String log_Name;
private String log_Ip;
private String log_Ip_Name;
private String log_Type;
private String Log_Content;
public String getLog_Content() {
return Log_Content;
}
public void setLog_Content(String log_Content) {
Log_Content = log_Content;
}
public String getLog_Id() {
return log_Id;
}
public void setLog_Id(String log_Id) {
this.log_Id = log_Id;
}
public String getLog_Time() {
return log_Time;
}
public void setLog_Time(String log_Time) {
this.log_Time = log_Time;
}
public String getLog_Name() {
return log_Name;
}
public void setLog_Name(String log_Name) {
this.log_Name = log_Name;
}
public String getLog_Ip() {
return log_Ip;
}
public void setLog_Ip(String log_Ip) {
this.log_Ip = log_Ip;
}
public String getLog_Ip_Name() {
return log_Ip_Name;
}
public void setLog_Ip_Name(String log_Ip_Name) {
this.log_Ip_Name = log_Ip_Name;
}
public String getLog_Type() {
return log_Type;
}
public void setLog_Type(String log_Type) {
this.log_Type = log_Type;
}
}
3、创建接口
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月6日 上午12:37:59
* 类说明
*/
package sumeng.dao;
import java.util.List;
import sumeng.etitly.Log;
/**
* @author Administrator
*
*/
public interface LogDao {
/**
* 查询所有日志
* @throws Exception
*/
List<Log> findAll() throws Exception;
}
4、实现接口
/**
* @author 作者 Your-Name:
* @version 创建时间:2019年5月6日 上午12:40:28
* 类说明
*/
package sumeng.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sumeng.dao.LogDao;
import sumeng.etitly.Log;
import sumeng.utils.BeanDao;
/**
* @author Administrator
*
*/
public class LogDaoImpl extends BeanDao implements LogDao{
public List<Log> findAll() throws Exception {
Connection connection = BeanDao.getConnection();
String sql = "select * from Log_table";
PreparedStatement prepareStatement = connection.prepareStatement(sql);
ResultSet rs = prepareStatement.executeQuery();
List<Log> list = new ArrayList<Log>();
while(rs.next()){
Log log = new Log();
log.setLog_Id(rs.getString(1));
log.setLog_Time(rs.getString(2));
log.setLog_Name(rs.getString(3));
log.setLog_Ip(rs.getString(4));
log.setLog_Ip_Name(rs.getString(5));
log.setLog_Type(rs.getString(6));
log.setLog_Content(rs.getString(7));
list.add(log);
}
BeanDao.closeAll(connection, prepareStatement, rs);
return list;
}
}
5、测试
