A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© LiuKang 中级黑马   /  2013-11-17 21:47  /  1612 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度
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;
}
}

评分

参与人数 1技术分 +1 收起 理由
狼王 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
继续努力哈,为了黑马,云7版块可以解决你的技术分问题
回复 使用道具 举报
狼王 发表于 2013-11-17 22:19
继续努力哈,为了黑马,云7版块可以解决你的技术分问题

非常感谢你的支持啊,我正在努力中。去了黑马大家多多交流哈。
回复 使用道具 举报
#在这里快速回复#谢谢楼主期望灌水,我是来拿技术分 Sun Feb 23 17:58:19 CST 2014
回复 使用道具 举报
#在这里快速回复#谢谢楼主期望灌水,我是来拿技术分 Sun Feb 23 20:33:10 CST 2014
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马