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 }
  • 相关阅读:
    定义模块与增加模块中的测试代码
    20150412自省
    Python中暂未解决的问题
    Node基础_文件系统
    Node基础_Buffer缓冲区
    Node基础_npm简介
    Node基础_模块化简介及详解
    Node基础_node简介
    Nosql_Mongoose简介及操作
    Nosql_MongoDB简单的查询练习
  • 原文地址:https://www.cnblogs.com/jj81/p/9852196.html
Copyright © 2011-2022 走看看