报错The url cannot be null什么情况?

来源:1-1 添加和删除字段操作

javaer11

2019-08-15 19:37:31

public class Demo {
   Connection connection=null;
   Statement statement=null;
   ResultSet resultSet=null;


   public static void main(String[] args) {
       Scanner scanner=new Scanner(System.in);
       System.out.println("user");
       int age= Integer.parseInt( scanner.nextLine());
       System.out.println("password");
       int num= Integer.parseInt( scanner.nextLine());
       boolean b=new Demo().login(age,num);
       if (b){
           System.out.println("登入成功");
       }else {
           System.out.println("no");
       }
   }
   public boolean login(int age,int num){
       try {
           connection = JDBCUtils.getConnection();
           String sql="select*from wer where age='"+age+"' and num='"+num+"'";
           statement= connection.createStatement();
          resultSet= statement.executeQuery(sql);
          return resultSet.next();
       } catch (SQLException e) {
           e.printStackTrace();
       }finally {
           JDBCUtils.close(resultSet,statement,connection);
       }
       return false;
   }
}

package cn.itcast;

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

/**
* JDBC工具类
*/
public class JDBCUtils {
   private static String url;
   private static String user;
   private static String password;
   private static String driver;
   /**
    * 文件的读取,只需要读取一次即可拿到这些值。使用静态代码块
    */
   static{
       //读取资源文件,获取值。

       try {
           //1. 创建Properties集合类。
           Properties pro = new Properties();

           //获取src路径下的文件的方式--->ClassLoader 类加载器
           ClassLoader classLoader = JDBCUtils.class.getClassLoader();
           URL res  = classLoader.getResource("jdbc.properties");
           String path = res.getPath();
           // System.out.println(path);///D:/IdeaProjects/itcast/out/production/day04_jdbc/jdbc.properties
           //2. 加载文件
           // pro.load(new FileReader("D:\\IdeaProjects\\itcast\\day04_jdbc\\src\\jdbc.properties"));
           pro.load(new FileReader(path));

           //3. 获取数据,赋值
           url = pro.getProperty("url");
           user = pro.getProperty("user");
           password = pro.getProperty("password");
           driver = pro.getProperty("driver");
           //4. 注册驱动
           Class.forName(driver);
       } catch (IOException e) {
           e.printStackTrace();
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
   }


   /**
    * 获取连接
    * @return 连接对象
    */
   public static Connection getConnection() throws SQLException {

       return DriverManager.getConnection(url, user, password);
   }

   /**
    * 释放资源
    * @param stmt
    * @param conn
    */
   public static void close(Statement stmt,Connection conn){
       if( stmt != null){
           try {
               stmt.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }

       if( conn != null){
           try {
               conn.close();
           } catch (SQLException e) {
               e.printStackTrace();
           }
       }
   }


   /**
    * 释放资源
    * @param stmt
    * @param conn
    */
   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();
           }
       }
   }

}

jdbc.properties文件

url=jdbc:mysql:///lkjtest
user=root
password=123456http://img.mukewang.com/climg/5d5543f20001dc8105690286.jpgdriver=com.mysql.jdbc.Driver

写回答

2回答

好帮手慕柯南

2019-08-16

同学你好!

是System.out.println(path);输出时乱码吗?同学可以按一下不收修改一下编码格式

file->setting,将下面编码都改为uft-8测试一下。

http://img.mukewang.com/climg/5d56590200016cde12301001.jpg

如果不可以建议同学使用英文路径,在今后的开发中也要使用英文路径,避免出现乱码问题。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

好帮手慕柯南

2019-08-16

同学的url里是不是有空格呢?同学看一下其它的参数是否能够获取到。

祝学习愉快~

0
havaer11
h 输出时候我的路径因为里面有中文,然后出现乱码,怎么解决这个问题
h019-08-16
共1条回复

0 学习 · 8016 问题

查看课程