正在加载……
2007-5-10
| |
Posted in 编程与开发 on 2007/05/10 / 引用(0)

ResultSet结果集合用集合类封装蛮好用的,我用Vector封装,不过Vector是同步的性能不一定好,可以选用其他的ArrayList等封装.
如下:
package com.shtour;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.util.*;
/**
* @author 小林信仁
*
*/
public class DBAction {

public static Connection getConnectionInstance(){
  
  return DBAction.getConn();
}

public static Connection getConn(){
  Connection conn = null;
  log("Create Connection begin..");
  try {
   Class.forName("com.mysql.jdbc.Driver");
     try {
      conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news","root","root");
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
   e.printStackTrace();
  }
  log("Create Connection end....");
  return conn;
}
public static Vector select(String sql) throws SQLException{
  Connection conn = DBAction.getConn();
  Statement stmt = conn.createStatement();
  Vector value = new Vector();
  ResultSet rs = stmt.executeQuery(sql);
  while(rs.next()){
   Vector temp = new Vector();
   for(int i =0; i < rs.getMetaData().getColumnCount(); i++){
    temp.add(rs.getString(1 + i).toString());
   }
   value.add(temp);
  
  }
  rs.close();
  stmt.close();
  conn.close();
  return value;
}
public static int insert(String sql) throws SQLException{
  Connection conn = DBAction.getConn();
  Statement stmt = conn.createStatement();
  int rows = stmt.executeUpdate(sql);
  return rows;
}
public static int delete(String sql) throws SQLException{
  Connection conn = DBAction.getConn();
  Statement stmt = conn.createStatement();
  int rows = stmt.executeUpdate(sql);
  return rows;
}
//控制台日志输出
public static void log(String str){
  System.out.println(str);
}
public static void main(String[] args) throws SQLException{
  Vector cc = DBAction.select("select * from tb_news_add_title");
}
}
表结构:
CREATE TABLE `tb_news_add_area` (
  `tid` int(11) NOT NULL auto_increment,
  `id` int(11) default NULL,
  `name` varchar(100) default NULL,
  `type` varchar(100) default NULL,
  PRIMARY KEY  (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

-- ----------------------------
-- Table structure for tb_news_add_title
-- ----------------------------
CREATE TABLE `tb_news_add_title` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;

This entry comes from 本站原创 and has been read for 761 times.It is tagged with .
huifon Homepage says:
at 2008/07/30 10:30
写得不好不是你的错,可写得不好还拿出来误人子弟,就是你的不对了。
呵呵,本人写java不久。人外有人 天外有天,敢请高手指教。博客本为个人所记录。
小林信仁 回复于 2008/07/30 14:40
分页: 1/1 第一页 1 最后页
发表评论

昵称

网址

电邮

OpenID登入 高级选项 表情