JAVA

学习Java第六天

今天完善了jdbc的报错和登陆功能并且周测了

jdbc我们完善了用户名登陆报错还有会出现的空指针异常的报错
代码如下:

package com.nbufe.test;

import java.sql.*;
import java.util.Scanner;

public class TestLogin {
    public static void main(String[] args) throws Exception {

            //1.加载驱动
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        Scanner input=new Scanner(System.in);
            try {
                Class.forName("com.mysql.jdbc.Driver")      ;
            }catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
        try {
            connection= DriverManager.getConnection
                    ("jdbc:mysql://127.0.0.1:3306/nbufe?useSSL=true&characterEncoding=utf-8&user=root&password=123");
            String sql="select * from user where username=? and password=?";
            statement=connection.prepareStatement(sql);
            String username,password;
            System.out.println("请输入用户名");
            username=input.next();
            System.out.println("请输入密码");
            password=input.next();
            statement.setString(1,username);
            statement.setString( 2,password);
            resultSet=statement.executeQuery();
           if (resultSet.next())
           {
               System.out.println("登陆成功");
           }
           else
               throw new LoginException("用户名密码错误");

        }catch (SQLException e)
        {
           e.printStackTrace();
        }finally {
            if(resultSet!=null)
            {
                try {
                    resultSet.close();
                }catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if (statement!=null) {
                try {
                    statement.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if (connection!=null)
            {
                try {
                    connection.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("111111");
    }


}

并且让我们预习了file创建文件,改文件名;

你可能感兴趣的:(JAVA)