This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes Reading usersnames/passwords from file for database access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Reading usersnames/passwords from file for database access" Watch "Reading usersnames/passwords from file for database access" New topic
Author

Reading usersnames/passwords from file for database access

p Walsh
Greenhorn

Joined: Nov 13, 2003
Posts: 14
Hi,
I have set up the java connection to mysql database.
Code works ok.. see below.....
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestQuery2 {
public TestQuery2 () {
}
public static void main(String args[]) {
String url="jdbc:mysql://localhost/publish";
Connection con;
String query = "SELECT * FROM book";
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Howdy"); }
catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
System.out.println("Trying to connect.fgndgjndfjgndfkgnd..");
System.out.println("Help");
con = DriverManager.getConnection(url,"publish","wrox");
System.out.println("Help before connection");
System.out.println("connected!");
stmt = con.createStatement();
ResultSet result = stmt.executeQuery(query);
while (result.next()) {
String name = result.getString(1) + " " + result.getString(2);
System.out.println(name);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}

The problem is that there is values hardcoded into above code,
eg Values such as username and password......
Is there an approach i can take to have this information read from a file...
rather than the following approach
File Login = new File ("Login Info.txt"); this file would contain usernames and passwords
FileInputStream FISLogin = new FileInputStream (Login)
BufferedReader fBr = new BufferedReader(new InputStreamReader(FISLogin));
Could you use props laod..
eg
Properties props.load(new FileInputStream("db.properties")); and if so ,how?
Thanks
Patrick
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Patrick,
You are on the right track with this. A properties file load is a very common way of doing things. A sample properties file would contain:

You get a properties file object. Then, you can use props.get("userid") and props.get("password") to get the values.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
p Walsh
Greenhorn

Joined: Nov 13, 2003
Posts: 14
Thanks Jeanne.
I have set up file object called db.properties
HOST=jdbc:mysql://localhost/
DATABASE=publish
UNAME=publish
PWORD=wrox
but i am receiving the error
Testsql1.java:40: incompatible types
found : void
required: java.util.Properties
props=props.load(new FileInputStream("db.properties"));
^
1 error

Do you have any ideas on this...
Please advise
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
public class Testsql1 {
public Testsql1 () {
}
public static void main(String args[]) {
Connection con;
String username;
String password;
String loc;
String dbase;
Properties props = new Properties();

String query = "SELECT * FROM book";
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("....."); }
catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
props=props.load(new FileInputStream("db.txt"));
loc=props.getProperty("HOST");
dbase=props.getProperty("DATABASE");
username=props.getProperty("UNAME");
password=props.getProperty("PWORD");
System.out.println("Help");
con = DriverManager.getConnection("HOST"+"DATABASE","UNAME","PWORD");
System.out.println("Help before connection");
System.out.println("connected!");
stmt = con.createStatement();
ResultSet result = stmt.executeQuery(query);
while (result.next()) {
String name = result.getString(1) + " " + result.getString(2);
System.out.println(name);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150


should be

the props.load() just loads the properties into the existing properties object. It doesn't return anything.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading usersnames/passwords from file for database access