zoukankan      html  css  js  c++  java
  • JSP | 基础 | 连接数据库

     1 package util;
     2 
     3 import java.sql.DriverManager;
     4 import java.sql.SQLException;
     5 
     6 import com.mysql.jdbc.*;
     7 
     8 
     9 
    10 public class DBConfgier {
    11                          
    12     private static final String driver = "com.mysql.jdbc.Driver";
    13     //定义连接URL、数据用户及密码
    14     private static final String dbURL = "jdbc:mysql://localhost:3306/School_info?characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
    15     private static final String dbUser = "root";
    16     private static final String dbPassword = "root";
    17             
    18     private static Connection conn = null;
    19     
    20     
    21     //载入JDBC驱动程序
    22     static
    23     {
    24         try
    25         {
    26             Class.forName("com.mysql.jdbc.Driver");
    27         } catch(Exception ex) {
    28             ex.printStackTrace();
    29         }
    30     }
    31     
    32     
    33     public static Connection getConnnection() throws Exception {
    34         if(conn == null) {
    35             conn =  (Connection) DriverManager.getConnection(dbURL, dbUser, dbPassword);
    36             return conn;
    37         }
    38         return conn;
    39     }
    40 
    41     public static void main(String[] args) throws Exception {
    42         Connection conn = DBConfgier.getConnnection();
    43         if(conn != null) {
    44             System.out.println("连接成功!");
    45         } else {
    46             System.out.println("连接失败!");
    47         }
    48     }
    49     
    50             
    51             
    52 }
  • 相关阅读:
    快速排序 C语言实现
    怎样认识女孩?
    看了你就想结婚!)
    C语言面试题大汇总
    VC6.0下载地址
    STL vector 容器介绍
    Beauty of Maths! Amazing!
    自我参考:Java学习的30个目标
    Beauty of Maths! Amazing!
    采用 Java 持久化框架:选择、时机和优缺点?
  • 原文地址:https://www.cnblogs.com/jj81/p/9852196.html
Copyright © 2011-2022 走看看