package cn.com.lzt.common.util.calendar; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JdbcConnection { // 数据库用户名 private static final String USERNAME = "root"; // 数据库密码 private static final String PASSWORD = "123456"; // 驱动信息 private static final String DRIVER = "com.mysql.jdbc.Driver"; // 数据库地址 private static final String URL = "jdbc:mysql://192.168.100.177:3306/hgl"; private static Connection connection; private static PreparedStatement preparedStatement; private static ResultSet resultSet; public JdbcConnection() { // TODO Auto-generated constructor stub try { Class.forName(DRIVER); System.out.println("数据库连接成功!"); } catch (Exception e) { } } /** * 获得数据库的连接 * * @return */ public static Connection getConnection() { try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } /** * 增删改; * * @param sql * @param obj * @return */ public static int executeUpdate(String sql, Object... obj) { connection = getConnection(); int result = 0; try { preparedStatement = connection.prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { preparedStatement.setObject(i + 1, obj[i]); } } result = preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); // 增删改返回数据,可以关闭,查询时要取数据,不可以关闭; } finally { close(); } return result; } /** * 查询; * * @param sql * @param obj * @return */ public static ResultSet executeQuery(String sql, Object... obj) { connection = getConnection(); try { preparedStatement = connection.prepareStatement(sql); if (obj != null) { for (int i = 0; i < obj.length; i++) { preparedStatement.setObject(i + 1, obj[i]); } } resultSet = preparedStatement.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return resultSet; } /** * 关闭 * * @author zbw 2017年10月4日 */ public static void close() { try { if (resultSet != null) { resultSet.close(); } if (preparedStatement != null) { preparedStatement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 增加、删除、改 * * @param sql * @param params * @return * @throws SQLException */ // public boolean updateByPreparedStatement(String sql, // Listparams)throws SQLException{ // boolean flag = false; // int result = -1; // pstmt = connection.prepareStatement(sql); // int index = 1; // if(params != null && !params.isEmpty()){ // for(int i=0; i 0 ? true : false; // return flag; // } /** * 查询单条记录 * * @param sql * @param params * @return * @throws SQLException */ // public Map findSimpleResult(String sql, List // params) throws SQLException{ // Map map = new HashMap(); // int index = 1; // pstmt = connection.prepareStatement(sql); // if(params != null && !params.isEmpty()){ // for(int i=0; i> findModeResult(String sql, // List params) throws SQLException{ // List> list = new ArrayList>(); // int index = 1; // pstmt = connection.prepareStatement(sql); // if(params != null && !params.isEmpty()){ // for(int i = 0; i map = new HashMap(); // for(int i=0; i T findSimpleRefResult(String sql, List params, // Class cls )throws Exception{ // T resultObject = null; // int index = 1; // pstmt = connection.prepareStatement(sql); // if(params != null && !params.isEmpty()){ // for(int i = 0; i List findMoreRefResult(String sql, List params, // Class cls )throws Exception { // List list = new ArrayList(); // int index = 1; // pstmt = connection.prepareStatement(sql); // if(params != null && !params.isEmpty()){ // for(int i = 0; i params = new ArrayList(); // params.add("小明"); // params.add("123xiaoming"); // params.add("张三"); // params.add("zhangsan"); // params.add("李四"); // params.add("lisi000"); // try { // boolean flag = jdbcUtils.updateByPreparedStatement(sql, params); // System.out.println(flag); // } catch (SQLException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // }*/ // // // /*******************删*********************/ // //删除名字为张三的记录 // /* String sql = "delete from userinfo where username = ?"; // List params = new ArrayList(); // params.add("小明"); // boolean flag = jdbcUtils.updateByPreparedStatement(sql, params);*/ // // /*******************改*********************/ // //将名字为李四的密码改了 // /* String sql = "update userinfo set pswd = ? where username = ? "; // List params = new ArrayList(); // params.add("lisi88888"); // params.add("李四"); // boolean flag = jdbcUtils.updateByPreparedStatement(sql, params); // System.out.println(flag);*/ // // /*******************查*********************/ // //不利用反射查询多个记录 // /* String sql2 = "select * from userinfo "; // List> list = jdbcUtils.findModeResult(sql2, null); // System.out.println(list);*/ // // //利用反射查询 单条记录 // String sql = "select * from userinfo where username = ? "; // List params = new ArrayList(); // params.add("李四"); // UserInfo userInfo; // try { // userInfo = jdbcUtils.findSimpleRefResult(sql, params, UserInfo.class); // System.out.print(userInfo); // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // // } }