首页 > 代码库 > JdbcUtils
JdbcUtils
import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; public class JdbcUtils { private static volatile JdbcUtils jdbcUtils = null; private static Connection conn = null; private static ResultSet rs = null; private static PreparedStatement ps = null; static String url = null; static String user = null; static String password = null; static { Properties p = new Properties(); try { p.load(JdbcUtils.class.getClassLoader().getResourceAsStream( "com/mysql/properties/mysql.properties")); url = p.getProperty("url"); user = p.getProperty("user"); password = p.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } private JdbcUtils() { getConn(); } private void getConn() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static JdbcUtils newInstance() { if (jdbcUtils == null) { synchronized (JdbcUtils.class) { if (jdbcUtils == null) { jdbcUtils = new JdbcUtils(); } } } return jdbcUtils; } public int insert(String sql, Object... obj) { int result = 0; try { ps = conn.prepareStatement(sql); int index = 1; for (Object o : obj) { ps.setObject(index++, o); } result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); return -1; } finally { close(); } return result; } public int Delete(String sql, Object... obj) { int result = 0; try { ps = conn.prepareStatement(sql); int index = 1; for (Object o : obj) { ps.setObject(index++, o); } result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); return -1; } finally { close(); } return result; } public int update(String sql, Object... obj) { int result = 0; try { ps = conn.prepareStatement(sql); int index = 1; for (Object o : obj) { ps.setObject(index++, o); } result = ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); return -1; } finally { close(); } return result; } public List<Map<String, Object>> select(String sql, Object... obj) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { ps = conn.prepareStatement(sql); int index = 1; for (Object o : obj) { ps.setObject(index++, o); } rs = ps.executeQuery(); ResultSetMetaData rsd = rs.getMetaData(); int length = rsd.getColumnCount(); // System.out.println(length); while (rs.next()) { Map<String, Object> m = new HashMap<String, Object>(); for (int i = 1; i <= length; i++) { m.put(rsd.getColumnName(i), rs.getObject(i)); } // System.out.println(m.size()); list.add(m); } } catch (SQLException e) { e.printStackTrace(); } finally { } return list; } private void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
JdbcUtils
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。