Results 1 to 19 of 19
  1. #1
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default read mysql connection configuration from config.properties

    Java Code:
    Properties properties = new Properties();
    Properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
    String conString = properties.getProperty("jdbc.url");
    String username = properties.getProperty("jdbc.username");
    String password = properties.getProperty("jdbc.password");
    I get error in second line but don't know what is wrong?

    help please

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: read mysql connection configuration from config.properties

    I get error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    This is the error:
    C:\LePOS_2\src\LePOS\BlagajnaVnosUpdater.java:25: error: <identifier> expected
    Properties.load(Thread.currentThread().getContextC lassLoader().getResourceAsStream("config.propertie s"));
    C:\LePOS_2\src\LePOS\BlagajnaVnosUpdater.java:25: error: <identifier> expected
    Properties.load(Thread.currentThread().getContextC lassLoader().getResourceAsStream("config.propertie s"));
    C:\LePOS_2\src\LePOS\BlagajnaVnosUpdater.java:25: error: ';' expected
    Properties.load(Thread.currentThread().getContextC lassLoader().getResourceAsStream("config.propertie s"));
    C:\LePOS_2\src\LePOS\BlagajnaVnosUpdater.java:25: error: illegal start of type
    Properties.load(Thread.currentThread().getContextC lassLoader().getResourceAsStream("config.propertie s"));
    C:\LePOS_2\src\LePOS\BlagajnaVnosUpdater.java:25: error: ';' expected
    Properties.load(Thread.currentThread().getContextC lassLoader().getResourceAsStream("config.propertie s"));

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: read mysql connection configuration from config.properties

    Did the error message have a ^ beneath the line where the error happened to located where in the line the error was?

    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    I try to read mysql configuration from config.properties file and save to String-s.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: read mysql connection configuration from config.properties

    What error messages do you get if you use the javac command to compile the program?

    The error messages you posted said the error was on line 25. The posted code does not show all the code. Can you post the first 30 lines of the program so we can see line 25 with the code that is in front of it?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    Here is the code:

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package LePOS;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    import java.io.InputStream;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.Properties;
    import javax.swing.table.DefaultTableModel;
    
    
    
    
    public class BlagajnaVnosUpdater {
        
    Properties properties = new Properties();
    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
    String conString = properties.getProperty("jdbc.url");
    String username = properties.getProperty("jdbc.username");
    String password = properties.getProperty("jdbc.password");
    
        
        public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
              
            try ( Connection con = (Connection) DriverManager.getConnection(conString,username,password);
           PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: read mysql connection configuration from config.properties

    OK. That shows the problem.
    Line 25 and following lines must be inside of a method. Line 24 is ok because it defines a variable and assigns it a value.
    The String variables can be defined outside of the method, but will have to be given values after load() is called inside of a method.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    I put line 25 inside of try. But code still not working.

  10. #10
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    All I wanna do is to read strings conString,username,password from file "config.properties". Is there a simple method to do this?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: read mysql connection configuration from config.properties

    still not working
    You need to copy the full text of the error message and paste it here if you want help with it.

    I wanna do is to read strings ... Is there a simple method to do this?
    Yes. You need to fix the errors in your program to be able to do that.
    Last edited by Norm; 11-19-2016 at 09:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: read mysql connection configuration from config.properties

    If you put the reading of the settings inside the try, then the values are still empty when the connection is created. You need to move those lines AFTER the method declaration and BEFORE the try:
    Java Code:
    public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
      Properties properties = new Properties();
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
      String conString = properties.getProperty("jdbc.url");
      String username = properties.getProperty("jdbc.username");
      String password = properties.getProperty("jdbc.password");
      
    try ( Connection con = (Connection) DriverManager.getConnection(conString,username,password);
           PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
    ...
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  13. #13
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    Quote Originally Posted by SurfMan View Post
    If you put the reading of the settings inside the try, then the values are still empty when the connection is created. You need to move those lines AFTER the method declaration and BEFORE the try:
    Java Code:
    public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
      Properties properties = new Properties();
      properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
      String conString = properties.getProperty("jdbc.url");
      String username = properties.getProperty("jdbc.username");
      String password = properties.getProperty("jdbc.password");
      
    try ( Connection con = (Connection) DriverManager.getConnection(conString,username,password);
           PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
    ...
    }
    Thanks again for help but code still not working as I want probably because I did not give you whole code.

    So here is whole code:
    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package LePOS;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.swing.table.DefaultTableModel;
    
    
    
    
    public class BlagajnaVnosUpdater {
        
        String conString ="jdbc:mysql://192.168.1.1:3306/database";
        String username ="1";
        String passward ="1";
        
        
        public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
              
            try ( Connection con = (Connection) DriverManager.getConnection(conString,username,passward);
           PreparedStatement UpdBlagajna = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
     
              UpdBlagajna.setString(1,datum);
              UpdBlagajna.setString(2,vnesel);
              UpdBlagajna.setString(3,prihodek);
              UpdBlagajna.setString(4,strosek);
              UpdBlagajna.setString(5,status);
              UpdBlagajna.setString(6,opis);
              byte[] bytes = Files.readAllBytes(Paths.get(path));
              UpdBlagajna.setBytes(7,bytes);
              UpdBlagajna.executeUpdate();
     
              return true;
              
          }catch(Exception e){
              e.printStackTrace();
          }
    
      
            return false;
            
        }
        
        public DefaultTableModel getData(){
            
            DefaultTableModel dm = new DefaultTableModel();
            dm.addColumn("ID");
            dm.addColumn("datum");
            dm.addColumn("vnesel");
            dm.addColumn("prihodek");
            dm.addColumn("strosek");
            dm.addColumn("status");
            dm.addColumn("opis");  
            
            String sql = "SELECT * FROM blagajnavnos";
            
            try{
                Connection con= (Connection) DriverManager.getConnection(conString, username, passward);
                
                Statement s =(Statement) con.prepareStatement(sql);
                
                ResultSet rs =s.executeQuery(sql);
                
                while(rs.next()){
                    String i = rs.getString(1);
                    String d = rs.getString(2);
                    String v = rs.getString(3);
                    String p = rs.getString(4);
                    String str = rs.getString(5);
                    String st = rs.getString(6);
                    String o = rs.getString(7);
                    
                    
                    dm.addRow(new String[]{i,d,v,p,str,st,o});
                    
                }
                
                return dm;
                
            }catch(Exception e){
                e.printStackTrace();
            }
            
            return null;
        }
    Code as is working ok, but I wanna read database connection settings (conString,username,password)from file config.properties. When I put in code like you said first half code working but second not.
    This is probably because string setting is inside method?

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: read mysql connection configuration from config.properties

    Your new code shows no reading from a file of any kind. For you it's probably the easiest to create a method to create a connection:
    Java Code:
    public Connection getConnection() throws Exception {
       Properties properties = new Properties();
       properties.load(this.getClass().getResourceAsStream("config.properties"));
       String conString = properties.getProperty("jdbc.url");
       String username = properties.getProperty("jdbc.username");
       String password = properties.getProperty("jdbc.password");
       return DriverManager.getConnection(conString,username,password);
    }
    Every time you need the connection, just call
    Java Code:
    Connection c = getConnection();
    Ofcourse, the mandatory exception handling is not included in the example and is left as excersise for the reader.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    Quote Originally Posted by SurfMan View Post
    Your new code shows no reading from a file of any kind. For you it's probably the easiest to create a method to create a connection:
    Java Code:
    public Connection getConnection() throws Exception {
       Properties properties = new Properties();
       properties.load(this.getClass().getResourceAsStream("config.properties"));
       String conString = properties.getProperty("jdbc.url");
       String username = properties.getProperty("jdbc.username");
       String password = properties.getProperty("jdbc.password");
       return DriverManager.getConnection(conString,username,password);
    }
    Every time you need the connection, just call
    Java Code:
    Connection c = getConnection();
    Ofcourse, the mandatory exception handling is not included in the example and is left as excersise for the reader.
    This worked like charm. Thanks again SurfMan

    I have another question. It is possible to call that method from for example MySqlConnect.java file?

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: read mysql connection configuration from config.properties

    Quote Originally Posted by lpt2007 View Post
    This worked like charm. Thanks again SurfMan

    I have another question. It is possible to call that method from for example MySqlConnect.java file?
    Yes, there are various ways of creating a connection like this. For simple applications, you can use a static factory:
    Java Code:
    final class MySQLConnect {
      public static Connection getConnection() throws Exception {
         Properties properties = new Properties();
         properties.load(MySQLConnect.class.getResourceAsStream("config.properties"));
         String conString = properties.getProperty("jdbc.url");
         String username = properties.getProperty("jdbc.username");
         String password = properties.getProperty("jdbc.password");
         return DriverManager.getConnection(conString,username,password);
      }
    }
    WIth the above code, you can just call MySQLConnect.getConnection() to get a connection. Note that there is no "this" in static so I replaced that with the class itself.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    This is exactly I was looking for. Thanks

    Can I ask another question?

    I wanna put this code in a file referent.java and then call it out when I need it. But I don't now how to put in JTextField as changing symbol? Is this possible?

    Here is CODE:
    Java Code:
    package LePOS;
    
    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.Statement;
    import java.sql.ResultSet;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author saso
     */
    final class referent {
     public void vnesel(){
            
            String sql = "SELECT  `First Name` , `Last Name` FROM `employee` WHERE  `online` LIKE  '1'";
            
            
            try {Connection con = (Connection) MySQLConnect.getConnection();
                
                
                Statement s = (Statement) con.prepareStatement(sql);
            
                
                ResultSet rs =s.executeQuery(sql);
              
                while(rs.next()){
                    jTextField.setText(rs.getString("First Name")+" "+rs.getString("Last Name"));
              
                }
            } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null,"Problem vnosa referenta");
                }   
        
    }

  18. #18
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: read mysql connection configuration from config.properties

    I don't get the part where you say: "put in JTextField as changing symbol".

    Few observataions:
    1) There is a useless catch block. The Exception disappears. You only get a JOptionPane message. During development, use at least e.printStacktrace()
    2) Are you sure those quotes (backticks) are working properly in the query?
    3) You are using a while-loop to run through your ResultSet. If your ResultSet contains 1000 rows, the JTextField will be updated 1000 times (which you don't see ofcourse, because it's a) fast, and b) probably not on the EDT), and only the last row value will be displayed in the JTextField. If those values are empty, then the JTextField will still be blank.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  19. #19
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: read mysql connection configuration from config.properties

    I use this part of code to check whitch employee is online and when employee add or change some record in database this code add record who changed the content of same row. When employee login, program add in employee table in online cell 1 and when logoff add in online cell 0.

    Because I will call this code a lot of time in my project I wanna call it from file like you show me how to call MySqlConnect from file.

    Later I wanna do this for multi employee online but I don't know yet how to check which user changed or added record in database If two or more employee are logged in program at the same time.

Similar Threads

  1. Replies: 10
    Last Post: 07-28-2015, 10:31 PM
  2. Replies: 0
    Last Post: 02-02-2012, 08:13 AM
  3. Replies: 1
    Last Post: 04-08-2009, 09:31 PM
  4. [SOLVED] web.xml config for database connection
    By simo_mon in forum Java Servlet
    Replies: 7
    Last Post: 02-10-2009, 09:45 AM
  5. How do i get connection string from struts-config.xml ?
    By Md Saim in forum Web Frameworks
    Replies: 1
    Last Post: 09-05-2008, 01:20 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
  •