package com.java1234.jdbc.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import com.mysql.jdbc.PreparedStatement; public class DbUtil { private static String dbUrl="jdbc:mysql://localhost:3306/db_book"; //"db_book"是数据库名字 private static String dbUserName="root"; //数据库用户名 private static String dbPassword="123456"; //数据库密码 private static String jdbcName="com.mysql.jdbc.Driver"; //驱动
//获取连接 public Connection getCon()throws Exception{ Class.forName(jdbcName); Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword); return con; }
//关闭连接 public void close(Statement stmt,Connection con)throws Exception{ if(stmt!=null){ stmt.close(); if(con!=null){ con.close(); } } }
//关闭连接 public void close(PreparedStatement pstmt,Connection con)throws Exception{ if(pstmt!=null){ pstmt.close(); if(con!=null){ con.close(); } } } }