欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

软工实习日记1

程序员文章站 2022-05-02 14:29:43
...

今天是软工实习的第一天, 主要的任务有进行JDBC的相关测试, 并且开始学习Spring框架的相关内容. 由于我有一段时间没有接触Java相关内容了, 所以先大致过了一遍关键点, 找了下感觉, 然后将JDBC连接测试完成, 下面会给出部分关键步骤

JDBC连接相关

版本:
jdk1.8.0_251
MySql 5.7.29
MySql Connector/ODBC 5.1.30

只给出部分关键代码

jdbc.properties
url=jdbc:mysql://localhost:3306/sqltest?useUnicode=true&characterEncoding=utf-8
user=root
password=123456
drive=com.mysql.jdbc.Driver
jdbc_test
import java.sql.*;
import java.util.Scanner;

/**
 * @Auther Jack
 * @Date 2020/7/20
 */
public class jdbc_test {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
     /*   Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sqltest?useUnicode=true&characterEncoding=utf-8","root","654321");
        String sql = "update jdbctable set name ='中文' where id = 2";
        Statement statement = connection.createStatement();
        int cout = statement.executeUpdate(sql);
        System.out.println(cout);
        statement.close();
        connection.close();*/
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username=sc.nextLine();
        System.out.println("请输入密码:");
        String password=sc.nextLine();
        boolean loginFlag = new jdbc_test().login(username,password);
        if(loginFlag){
            System.out.println("登录成功");
        }
        else {
            System.out.println("登录失败");
        }
    }
    public boolean login(String username ,String password){
        if(username==null||password==null){return false;}
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet =null;
        try {
            connection=JDBCUtils.getconnection();
            String sql = " select * from jdbctable where name='"+username+"' and password = '"+password+"'";
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            return resultSet.next();

        } catch (SQLException e) {
           e.printStackTrace();
        }
        finally {JDBCUtils.close(resultSet,statement,connection);
    }
        return false;
    }
}
JDBCUtils
import com.sun.xml.internal.ws.addressing.WsaActionUtil;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * @Auther Jack
 * @Date 2020/7/20
 */
public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String drive;
    static{
        try {
            Properties properties = new Properties();
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            System.out.println(path);
            //加载文件
            properties.load(new FileReader(path));
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            drive = properties.getProperty("drive");
            Class.forName(drive);
        }  catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static Connection getconnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    public static void close(ResultSet rs, Statement stmt,Connection conn) {
        if (rs!=null){
            try {
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

}
数据库内容

软工实习日记1

运行结果

软工实习日记1

相关标签: 软工实习