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 }
  • 相关阅读:
    cookie与session的区别
    基于TCP协议的网络编程
    springboot第一篇:springboot基础
    java中的正则表达式
    NIO
    io基础(字节流、字符流、转换流、缓冲字符流)
    基于UDP协议的网络编程
    es6.3学习笔记
    线程同步和线程通信
    java字符串各种编码
  • 原文地址:https://www.cnblogs.com/jj81/p/9852196.html
Copyright © 2011-2022 走看看