Reading Config File Contents - java

I am trying to read a "DB_Config_File.properties" file but for some reason the following error is occurring:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
My class that is handling the reading of the config file looks like the following:
public class database {
public static Connection conn;
public static void dbConnection() throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {
Properties props = new Properties();
driver = props.getProperty("driver");
String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password");
String configFile = "F:/Project/Java Project/src/mainScreen/DB_Config_File.properties";
InputStream ins = new FileInputStream(configFile);
props.load(ins);
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
}
Judging by the error I am guessing there is something wrong in the "Class.forName(driver)" part.
Can you please help me out here?
Thanks

You are loading the properties file from the input stream after you are reading values from it. The variable 'driver' is null as the properties are empty and is causing the NPE.

Related

Why does runtime loaded MySQL driver output "No suitable driver found"?

I tried to load MySQL driver when my application is running. So I made jar file loading method as "loadJobJars" to that be creating URLClassLoader object with given jar file list. And I was call Class.forName method and then DriverManager.getConnection method. But it was output "No suitable driver found" on terminal.
Is there anyone know why this be happened?
My code is below.
public DBConnectionManager(Path jarPath) throws MalformedURLException {
this.urlClassLoader = loadJobJars(listJarFiles(jarPath));
}
public Connection createConnection(String dbName, String host, int port, String db, String user,
String password) throws NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException, ClassNotFoundException, SQLException, InstantiationException {
dbName = dbName.toLowerCase();
String url = "jdbc:" + dbName + "://" + host + ":" + port + "/" + db;
String driverName = DBDriver.getDriverName(dbName.toUpperCase());
Logger.getInstance().info("connection url: "+url+" driver: "+driverName);
Class.forName(driverName, true, this.urlClassLoader);
Connection conn = DriverManager.getConnection(url, user, password); //Here is it was happened code line.
return conn;
}
public URLClassLoader loadJobJars(File[] jarFiles) throws MalformedURLException {
URL[] urls = Arrays.asList(jarFiles).stream().map(f -> {
try {
System.out.println(f.getAbsolutePath());
return f.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}).filter(u -> u != null).toArray(URL[]::new);
return URLClassLoader.newInstance(urls, ClassLoader.getPlatformClassLoader());
}
public File[] listJarFiles(Path jarPath) {
return jarPath.toFile().listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
}
Console output is below.
[INFO][DBConnectionManager:77][20210119152004] connection url: jdbc:mysql://localhost:3306/mysql driver: com.mysql.jdbc.Driver
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://192.168.1.157:3306/mysql
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
at com.innotree.innoquartz.iqjobgen.DBConnectionManager.createConnection(DBConnectionManager.java:35)
at com.innotree.innoquartz.iqjobgen.DBConnectionManager.main(DBConnectionManager.java:77)
The way DriverManager creates connections is sensitive to the class loader of the calling code. The Driver used to create the connection must be visible to the class loader of the calling class (or when the calling code has no class loader, the context class loader). The driver you loaded is not visible to the class loader of the calling code, so DriverManager will not use the driver to create a connection.
You must create the connection using code that uses the same class loader (or at least where the class loader with the driver is visible). Alternatively, you need to use the java.sql.Driver directly, skipping DriverManager altogether.
Thank for your kind answer.
I saw your answer and finally figure out the problem by direct loading MySQL driver. Your help was very useful to solve that. Solution code is down below.
String url = "jdbc:mysql://localhost:3306/mysql";
URLClassLoader loader = new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
Class cls = loader.loadClass("com.mysql.jdbc.Driver");
Driver driver = (Driver)cls.newInstance();
Properties info = new Properties();
info.put("user", "admin");
info.put("password", "1234");
Connection conn = driver.connect(url, info);

Setting up a remote Derby DB: "No suitable driver found" error

I am trying to set up a remote Derby database just for practice. The following code works without a problem whenever I access the DB on my harddrive:
class Test{
public static void main(String[] args) {
String protocol = "jdbc:derby:";
// String dbPath = "C:/Java_Practice/derbyDB"; // this dbPath works...
String dbPath = "//108.167.141.127/derbyDB"; // and this one doesn't
String url = protocol + dbPath;
try( Connection conn = DriverManager.getConnection(url) )
{
System.out.println(conn);
}
catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
I then uploaded the whole derbyDB directory to my Hostgator-hosted website, obtained its IP by pinging the server and edited the dbPath var accordingly. The code stopped working as if it can't even see the DB. What am I missing?
Looks like your driver class not loaded.
Try loading it before calling DriverManager.getConnection, and see if it works.
String driver = "org.apache.derby.jdbc.ClientDriver";
Class.forName(driver).newInstance();
String protocol = "jdbc:derby:";
String dbPath = "//108.167.141.127/derbyDB"+";create=true";
String url = protocol + dbPath;
Connection conn = DriverManager.getConnection(url);
Answering my own question after some digging.
This is what I found in the official Apache Derby documentation (https://db.apache.org/derby/docs/10.0/manuals/develop/develop14.html):
You can specify only databases that are local to the machine on which
the JVM is running.
Looks like what I wanted to accomplish cannot be done...

Database connection in Java Servlet [duplicate]

This question already has answers here:
How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
(13 answers)
Closed 7 years ago.
I'm trying to connect to a database in a Java Servlet, but I am unable to connect. I searched on google and each website is showing different way I don't know why.
Here is the code that I tried to connect, but nothing(data) is going to MySQL database rows.
public class DBConnection{
private static final long serialVersionUID = 1L;
private final Connection connection;
private final String dbURL = "jdbc:mysql://localhost:3306/Servlet";
private final String user = "root";
private final String pwd = "";
public DBConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
this.connection = DriverManager.getConnection(dbURL, user, pwd);
}
public Connection gC(){
return this.connection;
}
}
Here is I'm trying to insert data in database (but data is not adding to DB).
public class ReServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try{
String uName=req.getParameter("uName");
String uEmail=req.getParameter("uEmail");
String uPass=req.getParameter("uPass");
DBConnection conn=new DBConnection();
PreparedStatement ps;
ps = conn.gC().prepareStatement("insert into users(name,email,password) values (?,?,?)");
ps.setString(1, uName);
ps.setString(2, uEmail);
ps.setString(3, uPass);
ps.execute();
}catch(SQLException | ClassNotFoundException se){
se.printStackTrace();
}finally{
}
}
}
I also request to all, please give me simple and easy database connection example, about each website is showing with full registration or any other system. But I just want simple db connection example as I am trying in my code.
Edited
By replacing se.printStackTrace(); to this throw new RuntimeException(se); I'm getting this Exception java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
You nedd to register your driver with DriverManager like this:
Class c = Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver( c.newInstance() );
Please remember to close your connection in finally block.

Generate liquibase change log using java

I am trying to output a changelog xml file through java.
I am not sure as to what I need to pass as parameters to the following method.
liquibase.generateChangeLog("chris", changeLogWriter, outputStream, snapshotTypes);
Chris is the schema name in Oracle 11g XE.
I don't want to generate on command line. I want to use application I am building to generate it.
public class Test {
public static void main(String[] args) {
String driverName = "oracle.jdbc.driver.OracleDriver";
String dbURL = "jdbc:oracle:thin:#localhost:1521:xe";
String userName = "chris";
String userPwd = "Liberty123";
try {
Class.forName(driverName);
Connection c = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("success");
Liquibase liquibase = null;
try {
Database liqui_oracle = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(c));
liquibase = new Liquibase("", new FileSystemResourceAccessor(), liqui_oracle);
liquibase.generateChangeLog("chris", changeLogWriter, outputStream, snapshotTypes);

Connection issue in Hadoop MapReduce

In a Hadoop MapReduce program, I am trying to read JDBC connection details from connection.properties file, that is also in proper classpath.
Code:
public synchronized Connection getConnection() {
return getDetails(); // inside try catch block
}
public Connection getDetails() throws SQLException, IOException {
Properties props = new Properties();
FileInputStream in = new FileInputStream("connection.properties");
props.load(in);
in.close();
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null) {
System.setProperty("jdbc.drivers",drivers);
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password);
}
But when running my driver main program it gives an error:
java.io.FileNotFoundException: connection.properties (No such file or directory)
Any idea?
In order to run the MapReduce that uses properties file, you need to package it with your code, and instead of using the traditional reading the file streams from local system, you can read it from the jar you already built
reading properties from jar:
InputStream stream = this.getClass().getResourceAsStream("foo.properties"))

Categories

Resources