java连接mysql数据库(用户登陆测试)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40014593/article/details/82768009

java连接数据库需要驱动包:可以在百度下载,这个是我的连接分享下https://pan.baidu.com/s/1M4bsRaJ03Ocyb-aZSfdqdA

一,导入jar包:我是直接从eclipse导入的包

二,连接数据库

三,把输入的数据和查询的数据进行对比

四,逻辑判断用户名密码

package mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
/**
 * Mysql���ӹ���
 * */
public class MysqlConnector {

    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/shanshan";

    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "shan0825";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try{

            Scanner s=new Scanner(System.in);

            System.out.println("用户名:");

            String use1=s.nextLine();

            System.out.println("密码:");

            String mima=s.nextLine();

            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");

//             打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

//             执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            
            String sql;
            String sql1;
            //               查询数据;
            sql = "SELECT username FROM user where username='" +use1+"'";
            sql1="SELECT password FROM user where username='" +use1+"'";
//            
            System.out.println(sql);
                
            ResultSet rs = stmt.executeQuery(sql);
            //查询数据库用户名
            boolean next = rs.next();//判断写入            
//        
            if(next==true){
                String name=rs.getString("username");
                 rs = stmt.executeQuery(sql1);
                boolean next1=rs.next();
               
                if(next1==true){
                     String password=rs.getString("password");
                if(use1.equals(name)){
                 if(mima.equals(password))
                System.out.println("登陆成功");
                 else{
                        System.out.println("用户名或者密码错误");
                        }
            }
                }
                else{
                    System.out.println("用户名或者密码错误");
                }
            }
           else{
                    System.out.println("用户不存在");
                
            }

//完成后关闭
            rs.close();
            stmt.close();
            
            conn.close();

            System.out.println("Goodbye!");


        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }


    }
    //    }
}

猜你喜欢

转载自blog.csdn.net/qq_40014593/article/details/82768009