importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;/** mysql数据库操作
* @@author
* 创建时间:
* 更新时间:
**/
public classDBhandle {private static String driver = "com.mysql.jdbc.Driver";private static String url = "jdbc:mysql://192.168.6.71:3306/";private static String db = "test";private static String character = "?useUnicode=true&characterEncoding=gbk";private static String user = "linchaojiang";private static String pass = "qwsSedcRFV";static Connection conn = null;static Statement statement = null;static PreparedStatement ps = null;static ResultSet rs = null;
List list = new ArrayList();//返回所有记录
/** 连接数据库*/
public static voidconnDB() {try{
Class.forName(driver).newInstance();
conn= DriverManager.getConnection(url+db+character, user, pass);if (!conn.isClosed()) {
System.out.println("Succeeded connecting to MySQL!");
}
statement=conn.createStatement();
}catch(Exception e) {
e.printStackTrace();
}
}/** 关闭数据库*/
public static voidcloseDB() {if(rs != null){try{
rs.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}if(statement != null){try{
statement.close();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}if(conn != null){try{
conn.close();
System.out.println("Database connection terminated!");
}catch(SQLException e) {
e.printStackTrace();
}
}
}/** 查询数据表*/
publicList query(String sql){
connDB();intcount;try{
rs=statement.executeQuery(sql);
ResultSetMetaData rsmd;
rsmd=rs.getMetaData();
count=rsmd.getColumnCount();while(rs.next()){
Map map= newHashMap();for(int i=1;i<=count;i++){//获取指定列的表目录名称
String label=rsmd.getColumnLabel(i);//以 Java 编程语言中 Object 的形式获取此 ResultSet 对象的当前行中指定列的值
Object object=rs.getObject(i);//把数据库中的字段名和值对应为一个map对象中的一个键值对
map.put(label.toLowerCase(), object);
}
list.add(map);
}
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}returnlist;
}/** 数据插入*/
public voidinsert(String sql){
connDB();try{
statement.execute(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
}/** 数据更新*/
public voidupdate(String sql){
connDB();try{
statement.execute(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
}/** 数据删除*/
public voiddelete(String sql){
connDB();try{
statement.execute(sql);
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
}
}