0
public static JdbcTemplate connectBDD() {

  DriverManagerDataSource ds = new DriverManagerDataSource();

  ds.setDriverClassName("com.mysql.jdbc.Driver");

  ds.setUrl("jdbc:mysql://localhost:8080/test");

  ds.setUsername("root");

  ds.setPassword("root");

  JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);

  jdbcTemplate.setDataSource(ds);

  return jdbcTemplate;
}

Thanks to these lines of code I'm able to make queries to my database.

I've seen so many people doing the same thing using an xml file that contains all the information to connect to a database.

Can someone show me please how to write such a file and most importantly how to invoke it in the java code.

Thank you !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Can you give examples on where you found such JDBC xml configurations? – Selaron Oct 29 '18 at 15:13
  • the file contains more than 600 characters I cannot post it here, this is an extract from the file ... com.mysql.jdbc.Driver jdbc:mysql://linkToBdd/assrep?useSSL=false – Ousti Driss Oct 29 '18 at 15:20
  • Looks like someone is using the SpringFramework to configure her JDBC connection. – Selaron Oct 29 '18 at 15:22
  • How can I do such thing ? – Ousti Driss Oct 29 '18 at 15:24
  • I don't want to see the login in the java code – Ousti Driss Oct 29 '18 at 15:24
  • Scroll down to Quick Start at https://spring.io/projects/spring-framework an then Bootstrap your application with Spring Initializr: https://start.spring.io/ – Selaron Oct 29 '18 at 15:28
  • I would think that OP has seen a Hibernate implementation. That usually goes hand in hand with JDBC and is set up in a xml file. A complete Spring project is overkill here imho. – benji2505 Oct 29 '18 at 15:42
  • Can't see any hint pointing into Hibernat direction. How is a complete migration from direct JDBC access to using Hibernat not overkill? – Selaron Oct 29 '18 at 15:48

1 Answers1

0

If it is only that you don't want to have your username/password hard coded, you may want to read it from a properties file:

public static PropertyResourceBundle getProperties(final String fileName)
        throws FileNotFoundException, IOException {
    try (FileInputStream fis = new FileInputStream(fileName)) {
        return new PropertyResourceBundle(fis);
    }
}

public static JdbcTemplate connectBDD() throws FileNotFoundException, IOException {

    PropertyResourceBundle properties = getProperties("c:\\temp\\testapp.properties");

    DriverManagerDataSource ds = new DriverManagerDataSource();

    ds.setDriverClassName("com.mysql.jdbc.Driver");

    ds.setUrl("jdbc:mysql://localhost:8080/test");

    String userName = properties.getString("userName");
    ds.setUsername(userName);

    String password = properties.getString("password");
    ds.setPassword(password);

    JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);

    jdbcTemplate.setDataSource(ds);

    return jdbcTemplate;
}

With c:\temp\testapp.properties reading

userName=testUserWithNeededPrivelegesOnly
password=hardToGuessPassword
Selaron
  • 6,105
  • 4
  • 31
  • 39