db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/s1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
userName=root
password=52Java
连接公共类
package com.store.db;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class DBHelper {
private static String driver;
private static String url;
private static String userName;
private static String password;
static protected Connection conn;
protected PreparedStatement pstmt;
protected ResultSet rs;
/**
* 加载驱动,并建立数据库连接
*
* @return 返回数据库链接对象
* @throws SQLException 抛出SQLException
* @throws ClassNotFoundException 抛出ClassNotFoundException
* @throws IOException 抛出IOException
*/
static {
// 实例化Properties对象
Properties properties = new Properties();
// 加载properties配置文件
try {
properties.load(new FileInputStream(new File("project\\src\\com\\store\\db\\db.properties")));
} catch (IOException e) {
e.printStackTrace();
}
// 通过键名获取对应的值
driver = properties.get("driver").toString();
url = properties.get("url").toString();
userName = properties.get("userName").toString();
password = properties.get("password").toString();
}
/**
* 连接数据库公共类
*/
public static Connection getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
System.out.println("连接成功");
} catch (ClassNotFoundException e) {
System.out.println("没有找到驱动类");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
return conn;
}
/**
* 增删改公共类
*
* @param sql
* @param args
* @return
* @throws Exception
*/
public boolean update(String sql, Object... args) throws Exception {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
}
System.out.println(pstmt);//输出用来调试程序,做完注释
int i = pstmt.executeUpdate();
return i > 0 ? true : false;
}
/**
* 查询公共类
*
* @param sql
* @param args
* @return
* @throws Exception
*/
public ResultSet query(String sql, Object... args) throws Exception {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
}
rs = pstmt.executeQuery();
System.out.println(pstmt);//输出用来调试程序,做完注释
return rs;
}
/**
* 关数据库的公共方法
*/
public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
getConnection();
}
/**
* Java代码实现MySQL数据库导出
*
* @param userName 进入数据库所需要的用户名
* @param password 进入数据库所需要的密码
* @param savePathName 数据库导出文件保存路径加名字
* @param databaseName 要导出的数据库名
* @return 返回true表示导出成功,否则返回false。
*/
public static boolean backup(String userName, String password, String savePathName, String databaseName) {
try {
// String stmt = "mysql -udog -p52Java java95 < " + "c:/sql.sql";
String stmt = "mysqldump -u" + userName + " -p" + password + " " + databaseName + " > " + savePathName;
String[] cmd = {"cmd", "/c", stmt};
Process process = Runtime.getRuntime().exec(cmd);
if (process.waitFor() == 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
/**
* 操作结果:恢复数据库,前提是数据库里有该数据库名字,否则无法恢复(所以应该先创建一个数据库)
*
* @param username 用户名
* @param password 用户数据库密码
* @param databasename 数据库名字
* @param filePathName 数据库文件路径及名字加后缀
* @return boolean 如果恢复成功则返回true,否则返回false
*/
public static boolean recover(String username, String password, String databasename, String filePathName) {
try {
// String stmt = "mysql -uroot -padmin myDB < " + "c:/sql.sql";
String stmt = "mysql -u" + username + " -p" + password + " " + databasename + " < " + filePathName;
String[] cmd = {"cmd", "/c", stmt};
Process process = Runtime.getRuntime().exec(cmd);
if (process.waitFor() == 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
}