java JDBC连接mysql

  1. 下载驱动包:http://dev.mysql.com/downloads/connector/j/,解压得到jar文件,例如mysql-connector-java-8.0.11.jar
  2. 在项目下新建文件夹,将jar包放进去,点击项目右键,【构建路径】——【配置构建路径】,添加jar文件
  3. 连接数据库
  4. 加粗部分为新特性
  •  1 public final class DbConn
     2 {
     3     public static  Connection getconn()
     4     {
     5         Connection conn = null;
     6         
     7         String driver = "com.mysql.cj.jdbc.Driver";
     8         String user   = "root";
     9         String passwd = "root";
    10         String url = "jdbc:mysql://localhost:3306/store?useSSL=false&serverTimezone=UTC";//指定要访问的数据库store
    11         
    12         //已加载完驱动
    13         try
    14         {
    15             Class.forName(driver); 
    16             conn = DriverManager.getConnection(url,user,passwd);
    17         }catch (SQLException e)
    18         {
    19             e.printStackTrace();
    20         }
    21         catch (ClassNotFoundException e)
    22         {
    23             e.printStackTrace();
    24         }
    25         return conn;
    26     }
    27 
    28 }
     1 public final class DbClose
     2 {
     3     /**
     4      * 关闭 添加功能 资源
     5      * @param pstmt,rs,conn
     6      */
     7         public static void addClose(PreparedStatement pstmt, Connection conn)
     8         {
     9             /*
    10              * 多个 try-catch 出发点:安全
    11              */
    12             try
    13             {
    14                 if (pstmt != null)
    15                 {
    16                     pstmt.close();
    17                 }
    18             } catch (SQLException e1)
    19             {
    20                 e1.printStackTrace();
    21             }
    22             try
    23             {
    24                 if (conn != null)
    25                 {
    26                     conn.close();
    27                 }
    28             } catch (SQLException e)
    29             {
    30                 e.printStackTrace();
    31             }
    32         }
    33         
    34         /**
    35          * 关闭资源
    36          * @param pstmt,rs,conn
    37          */
    38         public static void queryClose(PreparedStatement pstmt, ResultSet rs, Connection conn)
    39         {
    40             try
    41             {
    42                 if (pstmt != null)
    43                 {
    44                     pstmt.close();
    45                 }
    46             } catch (SQLException e1)
    47             {
    48                 e1.printStackTrace();
    49             }
    50             try
    51             {
    52                 if (rs != null )
    53                 {
    54                     rs.close();
    55                 }
    56             } catch (SQLException e1)
    57             {
    58                 e1.printStackTrace();
    59             }
    60             try
    61             {
    62                 if (conn != null)
    63                 {
    64                     conn.close();
    65                 }
    66             } catch (SQLException e)
    67             {
    68                 e.printStackTrace();
    69             }
    70         }
    71         
    72 }

    测试代码。测试是否连接成功:

  •  1 public class Show {
     2 
     3     public static void main(String[] args) {
     4         //声明Connection对象
     5         Connection con;
     6         //驱动程序名
     7         String driver = "com.mysql.cj.jdbc.Driver";
     8         //URL指向要访问的数据库名mydata
     9         String url = "jdbc:mysql://localhost:3306/store?useSSL=false&serverTimezone=UTC";
    10         //MySQL配置时的用户名
    11         String user = "root";
    12         //MySQL配置时的密码
    13         String password = "root";
    14         //遍历查询结果集
    15         try {
    16             //加载驱动程序
    17             Class.forName(driver);
    18             //1.getConnection()方法,连接MySQL数据库!!
    19             con = DriverManager.getConnection(url,user,password);
    20             if(!con.isClosed())
    21                 System.out.println("Succeeded connecting to the Database!");
    22             //2.创建statement类对象,用来执行SQL语句!!
    23             Statement statement = con.createStatement();
    24             //要执行的SQL语句
    25             String sql = "select * from goods where Gid='g231'";
    26             //3.ResultSet类,用来存放获取的结果集!!
    27             ResultSet rs = statement.executeQuery(sql);
    28             System.out.println("-----------------");
    29             System.out.println("执行结果如下所示:");  
    30             System.out.println("-----------------");  
    31             System.out.println("名称" + "\t" + "数量");  
    32             System.out.println("-----------------");  
    33              
    34             String Gname = null;
    35             String Gnum = null;
    36             while(rs.next()){
    37                 //获取stuname这列数据
    38                 Gname = rs.getString("Gname");
    39                 //获取stuid这列数据
    40                 Gnum = rs.getString("Gnum");
    41 
    42                 //输出结果
    43                 System.out.println(Gname + "\t" + Gnum);
    44             }
    45             rs.close();
    46             con.close();
    47         } catch(ClassNotFoundException e) {   
    48             //数据库驱动类异常处理
    49             System.out.println("Sorry,can`t find the Driver!");   
    50             e.printStackTrace();   
    51             } catch(SQLException e) {
    52             //数据库连接失败异常处理
    53             e.printStackTrace();  
    54             }catch (Exception e) {
    55             // TODO: handle exception
    56             e.printStackTrace();
    57         }finally{
    58             System.out.println("数据库数据成功获取!!");
    59         }
    60     }
    61 
    62 }

     

posted @ 2018-06-05 17:21  0也做除数  阅读(230)  评论(0编辑  收藏  举报