package com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author vis.yang
 */
public class DBCon {

    public  Connection getConn() {
        //定义连接属性
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username="root";
        String password="123";
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBCon.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("驱动加载失败");
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            Logger.getLogger(DBCon.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;
    }
    //测试
    public static void main(String[] args) {
        DBCon dbCon = new DBCon();
        Connection conn = dbCon.getConn();
        if(conn!=null){
            System.out.println("ok");
        }else{
            System.out.println("fail");
        }
    }
}



package sqluntil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author vis.yang
 * @description 连接Mysql5的工具类
 * @help Mysql5的驱动名: "com.mysql.jdbc.Driver" 测试机:端口:3306 账号:root 密码:root
 */
public class C5 {

    public Connection getConn(String  dbPort,String dbName, String userName, String password) {
        //定义连接属性
        Connection conn = null;
        String url = "jdbc:mysql://localhost:"+dbPort+"/" + dbName + "?useUnicode=true&characterEncoding=utf8&useSSL=true";
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
        } catch (ClassNotFoundException ex) {
            System.out.println("驱动加载失败");
            Logger.getLogger(C5.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            Logger.getLogger(C5.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;
    }
//sample
//    public static void main(String[] args) {
//        C5 c5 = new C5();
//        Connection conn = c5.getConn("3306","people", "root", "root");
//        if (conn != null) {
//            System.out.println("Connection is ok!");
//        } else {
//            System.out.println("Connection is not prepare!");
//        }
//        try {
//            Statement statement = conn.createStatement();
//            String sql="select * from stu_info";
//            ResultSet re = statement.executeQuery(sql);
//            while(re.next()) {
//		 System.out.println("id="+re.getObject("id"));
//		 System.out.println("stuName="+re.getObject("stuName"));
//		 System.out.println("love="+re.getObject("love"));
//	 }
//        } catch (SQLException ex) {
//            System.out.println("执行对象创建失败");
//            Logger.getLogger(C5.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
}




package sqluntil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author vis.yang
 * @description 连接Mysql8的工具类
 * @help Mysql8的驱动名: "com.mysql.cj.jdbc.Driver" 测试机:端口:3308 账号:root 密码:root8
 */
public class C8 {

    public Connection getConn(String dbPort, String dbName, String userName, String password) {
        //定义连接属性
        Connection conn = null;
        String url = "jdbc:mysql://localhost:" + dbPort + "/" + dbName + "?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai";
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
        } catch (ClassNotFoundException ex) {
            System.out.println("驱动加载失败");
            Logger.getLogger(C5.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.out.println("数据库连接失败");
            Logger.getLogger(C5.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conn;
    }
//Sample
//    public static void main(String[] args) {
//        C8 c8 = new C8();
//        Connection conn = c8.getConn("3308", "student", "root", "root8");
//        try {
//            Statement statement = conn.createStatement();
//            String sql = "select * from stu_info";
//            ResultSet re = statement.executeQuery(sql);
//            while (re.next()) {
//                System.out.println("id=" + re.getObject("id"));
//                System.out.println("stuName=" + re.getObject("stuName"));
//                System.out.println("love=" + re.getObject("love"));
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(C8.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//    }
}