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;
says:
1




