• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading usersnames/passwords from file for database access

 
p Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34225
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
p Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34225
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

should be

the props.load() just loads the properties into the existing properties object. It doesn't return anything.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic