昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

Java操作数据库(粗糙版本或者说硬编码版本的)

在pom.xml文件中 添加如下代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cnblogs</groupId>
    <artifactId>HelloLogback</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>

    </dependencies>


</project>

源代码实现 p408

package com.cnblogs.HelloLogback.test;

import ch.qos.logback.classic.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

public class Driver {
    private static final Logger logger = LoggerFactory.getLogger(Driver.class);

    public static void main(String[] args) throws ClassNotFoundException {
//        这里强制类型转换时为了能设置 logger 的 Level : TRACE < DEBUG < INFO <  WARN < ERROR
        ch.qos.logback.classic.Logger logback_logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.cnblogs");
        logback_logger.setLevel(Level.TRACE);
        logback_logger.error("这是错误信息");
        logback_logger.warn("这是警告信息");

        logger.error("这是一个错误信息");
        logger.warn("这是一个警告信息");


        try {
//           加载Mysql 数据库驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String dburl = "jdbc:mysql://127.0.0.1:3306/webstore?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
        String sql = "select * from products";

        try {
//            建立链接对象
            Connection connection = DriverManager.getConnection(dburl, "root", "123456");
//            创建语句对象
            Statement statement = connection.createStatement();
//            执行查询语句
            ResultSet resultSet = statement.executeQuery(sql);
// 获取 SQL语句执行的结果
            while (resultSet.next()) {
                System.out.println(resultSet.getInt(1) + "\t" +
                        resultSet.getString(2) + "\t" +
                        resultSet.getString(3) + "\t" +
                        resultSet.getFloat(4) + "\t" +
                        resultSet.getInt(5)

                );
                logger.info("{} {} {} {} {}",resultSet.getInt(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getFloat(4),
                        resultSet.getInt(5)
                );
            }
            
//            关闭建立的对象   释放资源
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        


    }
}


posted on 2019-04-01 11:13  Indian_Mysore  阅读(197)  评论(1编辑  收藏  举报

导航