Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

mysql - How can I setting datasource in Spring application

I have a simple application with Spring-MVC and J2EE. Now I want to config the datasource. I want use my-SQL and I don't want use hibernate. So I have write a java class (visualizzazioni.java) to connect at mysql database. So the name of database, username, password for now are static. I want to get this parameter from a config file. How can I configure mysql in spring???

This is the my visualizzazioni.java:

package com.springmvcapp.manager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.swing.JPanel;

import com.springmvcapp.utility.Propr;

public class Visualizzazioni extends JPanel{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String nomeDB;       // Nome del Database a cui connettersi
    private String nomeUtente;   // Nome utentStringe utilizzato per la connessione al Database
    private String pwdUtente;    // Password usata per la connessione al Database
    private String errore;       // Raccoglie informazioni riguardo l'ultima eccezione sollevata
    private String host;
    public static Connection db;       // La connessione col Database
    private boolean connesso;    // Flag che indica se la connessione è attiva o meno
    public Properties properties=null;
    public Visualizzazioni() { 

        this.nomeDB= "spring_mvc";
        this.pwdUtente="password";
        this.nomeUtente="root";
        this.host= "localhost";

        connesso = false;
        errore = "";
    }

    // Apre la connessione con il Database
    public boolean connetti() throws InstantiationException, IllegalAccessException{
        connesso = false;
        //log = new MyLog4J();
        try {
            // Carico il driver JDBC per la connessione con il database MySQL
            Class.forName("com.mysql.jdbc.Driver");
            // Controllo che il nome del Database non sia nulla
            if (!nomeDB.equals("")) {
                //192.168.1.157
                this.pwdUtente="password";//properties.getProperty("root");
                this.nomeUtente="root";//properties.getProperty("user");

                //InitialContext ctx = new InitialContext();
                //DataSource ds = (DataSource) ctx.lookup("java:mysql/jdbc/spring_mvc");


                db = DriverManager.getConnection("jdbc:mysql://"+ host +"/" + nomeDB + "?user=" + nomeUtente + "&password=" + pwdUtente);
                connesso = true;
            } else {
                System.exit(0);
            }
        } catch (Exception e) { 
            //log.logStackTrace(e);
        }
        return connesso;
    }

    public static Connection getDb() {
        return db;
    }

    public static void setDb(Connection db) {
        Visualizzazioni.db = db;
    }

    public boolean isConnesso() { return connesso; }   // Ritorna TRUE se la connessione con il Database è attiva
    public String getErrore() { return errore; }       // Ritorna il messaggio d'errore dell'ultima eccezione sollevata
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get values from property file you need to do it like this

ResourceBundle sqlConnectionFileResource = ResourceBundle.getBundle("configFile");
String value=sqlConnectionFileResource.getString("propertyName");

where the file from which you want to pick up values is configFile.properties having entries in format

propertyName=propertyValue

remember to give only file name in getBundle method i.e. use only ResourceBundle.getBundle("configFile");

instead of

ResourceBundle.getBundle("configFile.properties");

hope this helps!

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...