Results 1 to 5 of 5
  1. #1
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Problem with read file and connect to mysql

    Hi,

    Today I have another problem. This time I want connect to mysql db. When I do like in tutorial and write everything in "public static void main(String[] args)" everything is good. But now I want read connection properties from file and connect to datebase and then run insert.
    I dont have error or warrings. Nothing happens. When I dispaly variable from file I hvae null and I dont know why ?

    Java Code:
    package aplikacja.mysql;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Mysql {
    
    	private String host;
    	private String username;
    	private String password;
    	private String db;
    
    	public void readConnectionParam() throws FileNotFoundException, IOException {
    		Properties mysqlAplikacjaProperties = new Properties();
    		FileInputStream mysqlPlik = new FileInputStream("aplikacja.properties");
    		mysqlAplikacjaProperties.load(mysqlPlik);
    		host = mysqlAplikacjaProperties.getProperty("jdbc.host");
    		username = mysqlAplikacjaProperties.getProperty("jdbc.username");
    		password = mysqlAplikacjaProperties.getProperty("jdbc.password");
    		db = mysqlAplikacjaProperties.getProperty("jdbc.db");
    	}
    
    	public void Connection() throws SQLException {
    		Connection polaczenie = null;
    		String driver = "com.mysql.jdbc.Driver";
    		String url = "jdbc:mysql://" + host + "/";
    		String dbName = db;
    		String userName = username;
    		String userPassword = password;
    
    		try {
    			Class.forName(driver).newInstance();
    			polaczenie = DriverManager.getConnection(url + dbName, userName,
    					userPassword);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		Statement statement = polaczenie.createStatement();
    		String command = "INSERT INTO users (id, name) VALUES (2, 'Tom')";
    		statement.executeUpdate(command);
    	}
    
    	public static void main(String[] args) {
    	}
    }
    and aplikacja.properties:
    Java Code:
    jdbc.host=localhost:3306
    jdbc.username=root
    jdbc.password=12test3
    jdbc.db=test
    Thanks for help

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Problem with read file and connect to mysql

    Your main method is empty.

    Regards.

  3. #3
    mr.suchy is offline Member
    Join Date
    Feb 2013
    Posts
    17
    Rep Power
    0

    Default Re: Problem with read file and connect to mysql

    I know it's empty but correct me If I'm wrong but I dont want display any text or select. I just want o insert. So what If I want do something it must be in main method. Sorry for this silly question.

    Java Code:
    package aplikacja.mysql;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Mysql {
    
    	public String host;
    	public String username;
    	public String password;
    	public String db;
    
    
    	public void readConnectionParam() throws FileNotFoundException, IOException {
    		Properties mysqlAplikacjaProperties = new Properties();
    		FileInputStream mysqlPlik = new FileInputStream("aplikacja.properties");
    		mysqlAplikacjaProperties.load(mysqlPlik);
    		host = mysqlAplikacjaProperties.getProperty("jdbc.host");
    		username = mysqlAplikacjaProperties.getProperty("jdbc.username");
    		password = mysqlAplikacjaProperties.getProperty("jdbc.password");
    		db = mysqlAplikacjaProperties.getProperty("jdbc.db");
    	}
    
    
    	public void Connection() throws SQLException {
    		Connection polaczenie = null;
    		String driver = "com.mysql.jdbc.Driver";
    		String url = "jdbc:mysql://" + host + "/";
    		String dbName = db;
    		String userName = username;
    		String userPassword = password;
    
    		try {
    			Class.forName(driver).newInstance();
    			polaczenie = DriverManager.getConnection(url + dbName, userName,
    					userPassword);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		Statement statement = polaczenie.createStatement();
    		String command = "INSERT INTO users (id, name) VALUES (2, 'Tom')";
    		statement.executeUpdate(command);
    	}
    	
    	public static void main(String[] args) {
    		Mysql bazamala = new Mysql();
    		System.out.println(bazamala.username);
    	}
    }
    Now I have null in "username" and don't know why bcs this informatio is in file aplikacja.properties. Thanks for help.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Problem with read file and connect to mysql

    Java Code:
    Mysql bazamala = new Mysql();
    This is all you are doing.
    Your default constructor has no code.
    Nothing is calling those other methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Problem with read file and connect to mysql

    The starting point for any program is the main method regardless.

    With the code above you have initialized an instance of MySQL but the constructor does not call the methods automatically. Your code on line 53 calls the variable 'username' from the instance 'bazamala' but the returned value is 'null'. To change this you will need to make a call to the method readConnectionParam().

    Regards.

Similar Threads

  1. Replies: 5
    Last Post: 09-25-2012, 06:36 PM
  2. Replies: 4
    Last Post: 03-05-2012, 12:26 AM
  3. Problem with jtable, mysql and a JAR file
    By pflerman in forum AWT / Swing
    Replies: 5
    Last Post: 09-23-2011, 07:02 PM
  4. how to connect MySql with JSP
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-13-2010, 02:50 PM
  5. Can NOT connect to Mysql
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-18-2009, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •