Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Java Property File?
    primarykey
    data
    text
    <p>I need to use <code>.properties</code> file in Java to store database information. </p> <p>Here is my database connector class. It giving <code>NullPointerException</code>. What is the issue with my code ?</p> <p>Note that I haven't' assign those property file values. DB connection values are still hard coded.</p> <pre><code>import java.io.IOException; import java.io.InputStream; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; public final class Database { public Connection connection; private Statement statement; private Properties property; public static Database database; private Database() { String url = "jdbc:mysql://localhost:3306/"; String dbName = "edus"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = ""; try { InputStream is = Database.class.getClassLoader().getResourceAsStream( "config.properties"); property.load(is); System.out.println(property.getProperty("db_user")); System.out.println(property.getProperty("db_password")); System.out.println(property.getProperty("db_name")); Class.forName(driver).newInstance(); this.connection = (Connection) DriverManager.getConnection(url + dbName, userName, password); }catch (IOException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException e) { System.out.println("JDBC driver is missing"); } catch (InstantiationException | IllegalAccessException | SQLException e) { e.printStackTrace(); } } public static synchronized Database getDatabaseConnection() { if (database == null) { database = new Database(); } return database; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload