zoukankan      html  css  js  c++  java
  • 连接mysql

      总有一种情况,属于那种,代码明明很对,可是就是有意想不到的问题,找半天的bug,然后,关了eclipse 重新开,重新写

      就又对了,问题对的莫名其妙

      连接mysql:

    package util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    public class DBHelper {
        private static final String driver = "com.mysql.jdbc.Driver";
        private static final String url = "jdbc:mysql://localhost:3306/test";
        private static final String usernane = "root";
        private static final String password = "*****";
        private static Connection connection = null;
        static
        {
            try {
                Class.forName(driver);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public  static Connection getConnection() throws Exception
        {
            if (connection == null) {
                connection = DriverManager.getConnection(url,usernane,password);
                return connection;
            }
            return connection;
        }
        
        public static void main(String []args)
        {
            try
            {
                Connection connection = DBHelper.getConnection();
                if (connection!= null) {
                    System.out.println("数据库连接正常");
                }
                else
                {
                    System.out.println("数据库连接异常");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        
    }
  • 相关阅读:
    jQuery
    我的微信公众号被封了
    安装pillow
    win 10 无线标志都不出现
    msfvenom 摄像头
    Python list的定义和删改
    Python md5解密
    Python 同ip网站查询(制作网站接口)
    python 带参数运行
    Python 查看本机WiFi密码
  • 原文地址:https://www.cnblogs.com/2714585551summer/p/6085861.html
Copyright © 2011-2022 走看看