JAVA 常用代码块

持续更新最后更新日期 2020年6月30日

常用时间格式化

import java.text.SimpleDateFormat;
import java.util.Date;
/*
 * 常用日期格式化
 * */
public class TestSimpleDateFormat {
    public static void main(String[] args) {
        SimpleDateFormat sdp = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        Date d = new Date();
        String format = sdp.format(d);
        System.out.println(format);
    }
}

在这里插入图片描述

JAVA发送邮件

package cn.itcast.travel.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发邮件工具类
 */
public final class MailUtils {
    private static final String USER = "17051104543@163.com"; // 发件人称号,同邮箱地址
    private static final String PASSWORD = "BJKZVOCJKCOPRIGPIY"; // 如果是qq邮箱可以使户端授权码,或者登录密码

    /**
     *
     * @param to 收件人邮箱
     * @param text 邮件正文
     * @param title 标题
     */
    /* 发送验证信息的邮件 */
    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");

           /*
               使用qq邮箱
               props.put("mail.smtp.host", "smtp.qq.com");
               使用163邮箱
                props.put("mail.smtp.host", "smtp.163.com");
            */

            props.put("mail.smtp.host", "smtp.163.com");
            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);

            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws Exception { // 做测试用
        MailUtils.sendMail("1558150917@qq.com","你好,这是一封测试邮件,无需回复。","测试邮件");
        System.out.println("发送成功");
    }



}

java 数据库连接

  • 使用阿里的Druid

    1. 使用maven管理jar包,添加我们所需要是用的jar
     <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.1</version>
        </dependency>
    
    1. 开始写我们万金油的工具类
        import com.alibaba.druid.pool.DruidDataSourceFactory;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class JDBCUtils {
       private static DataSource ds;
       // 是用静态代码块 完成配置文件的加载
       static {
           try {
               Properties pro = new Properties();
               pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
               ds= DruidDataSourceFactory.createDataSource(pro);
    
           }catch (Exception e){
               e.printStackTrace();
           }
    
       }
    
       // 获取连接池方法
       public static DataSource getDataSource(){
           return  ds;
       }
    
       // 获取连接
       public static Connection getConnection() throws SQLException {
           return ds.getConnection();
       }
    
       //释放资源
       public static void  close(Statement st,Connection conn){
          close(null,st,conn);
       }
       public static void close(ResultSet rs,Statement st,Connection conn){
           if (rs!=null){
               try {
                   rs.close();
               }catch (SQLException e){
                   e.printStackTrace();
               }
           }
           if (st!=null){
               try {
                   st.close();
               }catch (SQLException e){
                   e.printStackTrace();
               }
           }
           if (conn!=null){
               try {
                   conn.close();
               }catch (SQLException e){
                   e.printStackTrace();
               }
           }
    
       }
    }
    
    1. 配置文件的编写
    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://192.167.7.240:3306/dril
    username=root
    password=123456
    # 初始化连接数量
    initialSize=5
    # 最大连接数
    maxActive=10
    # 最大等待时间
    maxWait=3000
    

    4.开始我们的测试

    	import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class TestCase {
        public static void main(String[] args) {
            DuridTest();
        }
    
        public static void DuridTest() {
            Connection conn = null;
            Statement perstm = null;
            try {
                conn = JDBCUtils.getConnection();
                String sql = "select * from user";
                perstm = conn.createStatement();
                ResultSet resultSet = perstm.executeQuery(sql);
                while (resultSet.next()){
                    int id = resultSet.getInt("id");
                    String name = resultSet.getString("name");
                    System.out.println("id: "+id+"   name: "+name);
                }
    
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    在这里插入图片描述

请我喝杯咖啡可好!

微信

在这里插入图片描述

支付宝

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值