java: MySql Connection using JDK 14.02

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 14.02
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类 mysql-connector-java-8.0.27.jar.
 * 数据库:MySQL Server 8.0
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuMysqlCon.java
 *
 * */
 
 
 
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
 
 
 
public class DuMysqlCon {
 
    Connection con;
    Statement stmt = null;
    //
    public Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); //com.mysql.cj.jdbc.Driver  com.mysql.jdbc.Driver访问需要相应的JAR版本,在安装的数据库下拷贝来引用
            System.out.println("数据库驱动加载成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8", "root", "7涂聚文");
 
            System.out.println("数据库连接成功");
            System.out.println(" 实例化Statement对象...");       // 执行查询
            stmt = con.createStatement();
            String sql;
            sql = "SELECT city_id,city,country_id  FROM city";
            ResultSet rs = stmt.executeQuery(sql);
 
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int cityid  = rs.getInt("city_id");
                String city = rs.getString("city");
                int countryid = rs.getInt("country_id");
 
                // 输出数据
                System.out.print("ID: " + cityid);
                System.out.print("  ,  城市名字: " + city);
                System.out.print("  , 区域cid: " + countryid);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            con.close();
 
 
 
 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 14.02
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类 mysql-connector-java-8.0.27.jar
 * 数据库:MySQL Server 8.0
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuCon.java
 *
 * */
 
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.cj.*;
import com.mysql.jdbc.*;
import java.io.*;
import java.lang.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.sql.*;
 
 
 
public class DuCon {
 
 
    Connection con;
    //private static Properties duproperties;
    private static Properties  pp = null;
    private static InputStream fis = null;
    private static String url = "";
    private static String  driverName="";
    private static String user="";
    private static String password ="";
 
    public Connection getConnection() {
 
        // duproperties =new Properties();//创建资源对象
        pp = new Properties();
        try {
            fis=DuCon.class.getClassLoader().getResourceAsStream("dbinfo.properties");
            pp.load(fis);
 
            url = pp.getProperty("url");
            driverName = pp.getProperty("driverName");
            user = pp.getProperty("user");
            password = pp.getProperty("password");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        //properties.load(new InputStreamReader(DuCon.class.getClassLoader().getResourceAsStream("dbinfo.properties"),"UTF-8"));
 
        try
        {
 
            Class.forName(driverName);
            System.out.println("数据库驱动加载成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 
        try {
            con = DriverManager.getConnection(url,user,password);
            System.out.println("数据库连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
 
 
}

  

dbinfo.properties:(放在SRC文夹下)
1
2
3
4
driverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/sakila
user=root
password=涂聚文

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 14.02
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类
 * 2021-12-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuMain.java
 *
 * */
 
 
 
 
import com.mysql.cj.*;
import com.mysql.jdbc.*;
// import com.intellij.database.*;
// import com.intellij.database.dialects.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
 
 
 
public class DuMain {
 
 
 
 
 
    public static void main(String[] args) {
        DuMysqlCon c = new DuMysqlCon();
        c.getConnection();
 
        DuCon cn=new DuCon();
        cn.getConnection();
 
 
    }
 
 
 
}

  

 

 

IntelliJ IDEA 2021.2.3 ,Eclipse IDE for Enterprise Java and Web Developers - 2021-09

 

 

posted @   ®Geovin Du Dream Park™  阅读(31)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-12-17 css:Media Queries
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示