一个BaseDao.java文件帮助大家简单操作mysql数据库
BaseDao.java代码块
代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static String driver="com.mysql.jdbc.Driver";//数据库驱动
private static String database;//数据库名
private static String url="jdbc:mysql://"; //url地址
private static String username;//用户名
private static String password;//密码
private static Connection con=null;//连接对象
private static PreparedStatement pstmt=null;//sql语句预处理对象
private static ResultSet rs=null;//查询生成的结果集
private static int rows=0;//执行增删改sql语句返回的影响的行数
/**
*
* @param linkUrl 数据库连接地址,本地连接为localhost
* @param port 端口号
* @param database 使用的数据库
* @param username 用户名
* @param password 密码
*/
public BaseDao(String linkUrl,int port,String database,String username,String password) {
this.database = database;
this.username = username;
this.password = password;
this.url += linkUrl+":"+port+"/"+database;
}
public static String getDriver() {
return driver;
}
public static void setDriver(String driver) {
BaseDao.driver = driver;
}
public static String getDatabase() {
return database;
}
public static void setDatabase(String database) {
BaseDao.database = database;
}
public static String getUrl() {
return url;
}
public static void setUrl(String url) {
BaseDao.url = url;
}
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
BaseDao.username = username;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
BaseDao.password = password;
}
/**
* 创建数据连接
* @return 返回连接
*/
private static Connection getCon() {
Connection con=null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
*
* @param sql 增删改语句
* @return 返回的数字表示影响了几行
*/
public static int exeUp(String sql) {
con=getCon();
try {
pstmt=con.prepareStatement(sql);
rows=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
closeResultSet(rs);
closeAll(rs);
}
return rows;
}
/**
* 执行增删改语句
* @param sql 查询语句
* @param objs objs数组存放sql语句中?对应的内容
* @return 返回的数字表示影响了几行
*/
public static int exeUp(String sql,Object [] objs) {
con=getCon();
try {
pstmt=con.prepareStatement(sql);
for(int i=0;i<objs.length;i++) {
pstmt.setObject(i+1, objs[i]);
}
rows=pstmt.executeUpdate();
if (rows>0) {
System.out.println("sql语句执行成功");
} else {
System.out.println("sql语句执行失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
closeResultSet(rs);
closeAll(rs);
}
return rows;
}
/**
* 执行查询语句
* @param sql 查询语句
* @return 执行成功返回结果集
*/
public static ResultSet exeQu(String sql) {
con=getCon();
try {
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 执行查询语句
* @param sql 查询语句
* @param objs objs数组存放sql语句中?对应的内容
* @return 执行成功返回结果集
*/
public static ResultSet exeQu(String sql,Object [] objs) {
con=getCon();
try {
pstmt=con.prepareStatement(sql);
for(int i=0;i<objs.length;i++) {
pstmt.setObject(i+1, objs[i]);
}
rs=pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 关闭ResultSet资源
* @param rs
*/
private static void closeResultSet(ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 关闭资源
* @param con 连接对象
* @param pstmt sql预处理对象
* @param rs 结果集
*/
public static void closeAll(ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(pstmt!=null) {
pstmt.close();
}
if(con!=null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 释放资源
* @param resources
*/
public static void releaseResources(AutoCloseable... resources) {
for (AutoCloseable resource : resources) {
if (resource!= null) {
try {
resource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
测试代码:
import java.sql.ResultSet;
public class Demo {
public static void main(String[] args) throws Exception {
//本地连接,数据库名:company,用户名:root,密码:123.
BaseDao bd = new BaseDao("localhost",3306,"company","root","123.");
//sql语句
String sql = "UPDATE partment SET part_name=? WHERE part_num=4";
Object[] objs = {"管理部"};//obj数组中存放sql语句中?对应的内容
bd.exeUp(sql,objs);//执行修改功能的sql语句
sql = "select * from partment";
ResultSet rs = bd.exeQu(sql);//执行查询,返回结果集
//查询结果集内容
while (rs.next()) {
System.out.println(rs.getInt("part_num")+"\t"+rs.getString("part_name"));
}
//关闭数据库连接资源
bd.closeAll(rs);
}
}
注:如果代码有错误,欢迎留言!