封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度
package com.lk.DataBase;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.Vector;
public class RayiData {
/**
* 数据库操作成功
*/
public static final String SUCCESS = "SUCCESS";
/**
* 数据库操作失败
*/
public static final String ERROR = "ERROR";
private static Connection conn = null;
private String driverName = "";
private String url = "";
private String username = "";
private String password = "";
private RayiData() {
createConnection();
}
/**
* 创建RayiData对象
*
* @return
* RayiData对象
*/
public static RayiData createRayiData() {
return new RayiData();
}
/**
* 关闭RayiData对象
*/
public static void closeRayiData() {
if (conn != null)
try {
if (!conn.isClosed())
conn.close();
} catch (SQLException e) {
System.out.println("关闭RayiData错误 " + e);
}
}
private void createConnection() {
readConfig();
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println("连接数据库错误 " + e);
}
}
/**
* 读取属性文件 RayiDataBase.properties 1.DriverName 2.URL 3.UserName 4.PassWord
*/
private void readConfig() {
try {
String path = System.getProperty("user.dir")
+ "\\RayiDataBase.properties";
System.out.println("属性文件路径:" + System.getProperty("user.dir"));
FileInputStream is = new FileInputStream(path);
Properties props = new Properties();
props.load(is);
this.driverName = props.getProperty("DriverName");
this.url = props.getProperty("URL");
this.username = props.getProperty("UserName");
this.password = props.getProperty("PassWord");
} catch (Exception e) {
System.err.println("读取属性文件出错. " + e);
}
}
/**
* 对数据库进行操作
*
* @param sql
* SQL语句
* @return 执行成功--RayiData.SUCESS 执行失败--RayiData.ERROR
*/
public String updateDB(String sql) {
String re = RayiData.SUCCESS;
try {
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
if (stmt != null)
stmt.close();
} catch (Exception e) {
re = RayiData.ERROR + "\n" + e;
}
return re;
}
/**
* 读取数据库的内容
*
* @param sql
* 需执行的sql语句
* @return 返回查询的结果集 行、列从1开始编号 行数为[0][0] 列数为[0][1]
* @throws Exception
*/
public String[][] searchDB(String sql) {
String[][] data = null;
int rows = 0;
int columns = 0;
Vector vc = new Vector();
Statement st = null;
try {
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
columns = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columns; i++)
vc.add(rs.getString(i));
rows++;
}
if (rs != null)
rs.close();
if (st != null)
st.close();
} catch (SQLException e) {
System.out.println("查询语句 " + sql + " 执行错误 " + e);
}
data = new String[rows + 1][columns + 1];
if (columns != 0) {
data[0][0] = String.valueOf(rows);
data[0][1] = String.valueOf(columns);
}
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= columns; j++)
data[i][j] = (String) vc.get((i - 1) * columns + (j - 1));
vc = null;
return data;
}
} |