Java JDBC连接数据库
package cn.cqvie.yjq; import java.sql.*; /** * 注册数据库的驱动程序,并得到数据库的连接对象 * @author yu * */ public class DBUtil { static String DriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=book"; static String USER = "sa"; static String PASSWORD = "123"; //静态代码块,只执行一次 static { try { Class.forName(DriverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //得到连接对象 public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(URL,USER,PASSWORD); } catch (SQLException e) { e.printStackTrace(); } return conn; } //关闭连接 public static void free(ResultSet rs,Statement stmt, Connection conn) { if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if(stmt != null) { try { stmt.close(); } catch (Exception e2) { e2.printStackTrace(); } finally { if(conn != null) { try { conn.close(); } catch (Exception e3) { e3.printStackTrace(); } } } } } } /* if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } */ } }
使用JDBC实现增删改查的功能
package cn.cqvie.yjq; import java.sql.*; import java.util.*; public class SQLHelper { /** * 根据Connection,带坑语句,所有坑的值params来生成一个具体的PreparedStatement语句 * @param conn * @param cmdText * @param params * @return * @throws SQLException */ public static PreparedStatement getPreparedStatement(Connection conn,String cmdText,Object... params) { if(conn == null) { return null; } PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(cmdText); int i = 1; if(params != null) { for(Object obj:params) { if(obj != null) { pstmt.setObject(i, obj); } i ++; } } //DBUtil.free(null, null, conn); return pstmt; } catch (SQLException e) { e.printStackTrace(); DBUtil.free(null, pstmt, conn); } return null; } /** * 查询一个结果集 * @param conn * @param cmdText * @param params * @return * @throws SQLException */ public static List<Object[]> executeQuery(Connection conn,String cmdText,Object...params) { PreparedStatement pstmt = getPreparedStatement(conn, cmdText, params); ResultSet rs = null; List<Object[]> list = new ArrayList<Object[]>(); try { rs = pstmt.executeQuery(); //从rs中获取每一个行数据的列的个数 ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); //定义一个保存每一行的各个列的值的容器,Object[] Object[] objects = null; while(rs.next()) { objects = new Object[columnCount]; for(int i = 0;i < columnCount;i ++) { objects[i] = rs.getObject(i + 1); } list.add(objects); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.free(rs, pstmt, conn); } return list; } /** * 查询一个具体的值 * @param conn * @param cmdText * @param params * @return * @throws SQLException */ public static Object executeScalar(Connection conn,String cmdText,Object...params) { Object obj = null; if(conn == null) { return null; } List<Object[]> list = executeQuery(conn, cmdText, params); if(list != null && list.size() != 0) { obj = list.get(0)[0]; return obj; } return null; } /** * 增删改方法 * @param conn * @param cmdText * @param params * @return * @throws SQLException */ public static int executeUpdate(Connection conn,String cmdText,Object...params) { if(conn == null) { return -2; } PreparedStatement pstmt = getPreparedStatement(conn, cmdText, params); int rows = 0; try { rows = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.free(null, pstmt, conn); } return rows; } }