java关于jdbc后台数据库连接

关于数据库连接,想接触后台的同学肯定都会先想到这个,对于这块基本是个定死的东西,基本都是按部就班,当然如果你有兴趣可以研究研究这么才能跟方便快捷的连接,

下面的是关于本人的一些代码连接   基本只要你加载了驱动包,那么改掉密码用户名  就可以使用

public class DBConnection {
private static final String DBDRIVER="com.mysql.jdbc.Driver";//驱动加载
private static final String DBURL="jdbc:mysql://localhost:3306/db_affairmanage";
private static final String DBUSER="root";
private static final String DBPASSWORD="root";
public static Connection getConnection(){
 Connection conn=null;
 try{
  Class.forName(DBDRIVER);
  conn=DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
 }catch(ClassNotFoundException e){
  e.printStackTrace();//捕获驱动异常
 } catch (SQLException e) {
  e.printStackTrace();
 }
 return  conn;
}
public static void close(Connection conn){
 if(conn!=null){
  try{
   conn.close();
  }catch(SQLException e){
   e.printStackTrace();
  }
 }
}
public static void close(PreparedStatement pstmt){//关闭预处理对象
 if(pstmt!=null){
  try{
   pstmt.close();
  }catch(SQLException e){
   e.printStackTrace();
  }
 }
}

关于如何加载jdbcmysql驱动包的  相信网上很多文章有写   我这只是让各位更方便罢了。

posted @ 2016-03-13 18:05  DaviLin  阅读(291)  评论(0编辑  收藏  举报