zoukankan      html  css  js  c++  java
  • java连接MySQl数据库

    package com.abc.dao;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class BaseDao {
     public Connection getConn()
     {
      Connection conn=null;
      try {
       Class.forName("com.mysql.jdbc.Driver");
      } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      String url="jdbc:mysql://127.0.0.1:3306/user";
      try {
       conn=DriverManager.getConnection(url, "root", "");
      } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }  
      return conn;
     }
     public void closeAll(ResultSet rs,Statement stat,Connection conn)
     {
      if(rs!=null)
       try {
        rs.close();
       } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      if(stat!=null)
       try {
        stat.close();
       } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      if(conn!=null)
       try {
        conn.close();
       } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
     }
    }

  • 相关阅读:
    [Java复习] 缓存Cache part2
    [Java复习] 多线程 并发 JUC 补充
    [Java复习] 缓存Cache part1
    [Java复习] MQ
    [Java复习] 设计模式 Design Pattern
    [Java复习] Spring Cloud
    [Java复习] Spring Boot
    [Java复习] JVM
    [Java复习] Spring 常见面试问题
    关于nginx的源码安装方式
  • 原文地址:https://www.cnblogs.com/zjiacun/p/3350133.html
Copyright © 2011-2022 走看看