import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; /** * <B>文件名称:</B>DBUtils.java<BR> * <B>文件描述:</B><BR> * * <B>版权声明:</B>(C)2014-2015<BR> * <B>公司部门:</B>java研发部 <BR> * <B>创建时间:</B>2015年6月1日<BR> * * @author ggf * @version */ public class DBUtils { public static final String url = "jdbc:mysql://127.0.0.1/lvyun"; public static final String name = "com.mysql.jdbc.Driver"; public static final String user = "root"; public static final String password = "root"; //开启连接 public static Connection creatConnection(){ Connection conn = null; try { Class.forName(name); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //执行语句 public PreparedStatement getps(Connection conn,String sql){ PreparedStatement ps = null; try { ps = conn.prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ps; } //关闭Connection public static void close(Connection conn) { if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } //关闭PreparedStatement public static void close(PreparedStatement ps) { if(ps!=null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
时间: 2024-11-07 02:02:28