JDBC连接类
2018-07-20 来源:open-open
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; 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(); } } } }
标签: Mysql
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇: ImageView显示网络图片资源
下一篇:ios怎么判断日期是周末?
最新资讯
热门推荐