Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Reading usersnames/passwords from file for database access

 
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
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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
Posts: 40035
809
Eclipse IDE VI Editor Java
  • 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.
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic