Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- * 1* @author Administrator
- * 1*
- * 1
- */
- public class DBUtil {
- public Connection connectMysql() {
- Connection conn = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("成功加载MySQL驱动!");
- }catch(ClassNotFoundException e1){
- System.out.println("找不到MySQL驱动!");
- e1.printStackTrace();
- }
- String url="jdbc:mysql://localhost:3306/test0";
- try {
- conn = DriverManager.getConnection(url, "root","00000000");
- System.out.print("成功连接到数据库!");
- } catch (SQLException e){
- e.printStackTrace();
- }
- finally {
- return conn;
- }
- }
- public void connClose(Connection connection){
- try {
- connection.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- DBUtil dbUtil =new DBUtil();
- }
- }
Add Comment
Please, Sign In to add comment