java programming and database connection

Asked

Viewed 163 times

1

I have a problem that’s making me miserable. Here’s the thing:

I have a database and have my program in java everything connected, already created the jar file and all.

But the problem is that the program only runs on the machine perfectly on the machine in which the database is installed, I would like to know how to ensure that the application will continue to save data without however having the database installed.

Basically I need to know somehow how to carry the database along with my app. package.ie.work.;

import java.sql.Connection; import java.sql.Drivermanager; import java.sql.Resultset; import java.sql.Sqlexception; import java.sql.Statement;

public class Connexaooracle {

private String NomeDoUsuario;
private String Senha;
private String Caminho;

private final String host = "localhost";
private final String servico = "xe";
private final String PortaDeEntrada = "1521";


public Connection Connexao;

public ConnexaoOracle(String nome, String Senha) {

    setNomeDoUsuario(nome);;
    setSenha(Senha);
    setCaminho();

}

public void setNomeDoUsuario(String Nome){
    this.NomeDoUsuario =Nome;
}
public void setSenha(String senha){
    this.Senha = senha; 
}
private void setCaminho() {
    Caminho = "jdbc:oracle:thin:@"+host + ":" + PortaDeEntrada + ":" + servico; 

}

public String getSenha()
{
    return this.Senha;
}

public String getNomeDoUsuario() {
    return NomeDoUsuario;
}
public String getCaminho() {
    return Caminho;
}



public boolean Connectar()
{
    boolean vereficadorDeConexao = false;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        Connexao = DriverManager.getConnection(getCaminho(), getNomeDoUsuario(), getSenha());
        vereficadorDeConexao = true;

    } catch (InstantiationException | IllegalAccessException
            | ClassNotFoundException | SQLException e) {

        vereficadorDeConexao = false;
    } 
    return vereficadorDeConexao;
}

public ResultSet ExecutarComandoSql(String Comando)
{
    ResultSet Resultados = null ;
    try {
        Statement Comandosql = Connexao.createStatement();
        Resultados = Comandosql.executeQuery(Comando.toUpperCase());


    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return Resultados;  
}

public void commit()
{

    String Comando = "commit";
    ExecutarComandoSql(Comando );

} 

public void rollback()
{
    String Comando = "rollback";
    ExecutarComandoSql(Comando );

}
public boolean Disconnectar()
{
    boolean vereficadorDeDesconexao = false;
    try {
        Connexao.commit();
        Connexao.close();
    } catch (SQLException e) {

    }

    vereficadorDeDesconexao = true;
    return vereficadorDeDesconexao;
}

}

  • 1

    Using which database? Is a desktop application using swing? Add more details to the question.

  • Enter the code or database configuration file.

1 answer

1


Your need is for a Embedded Database, is an integrated application bank that also runs next to the application.

As you can see on wikipedia, there are several options, the difference is like the storage modes, on disk, in memory or a combination of both, make sure you don’t end up losing dice.

Usually in memory it is used in development environment for being faster and simpler to test as data is erased each time the application is closed.

But usually the disk storage mode is what happens in separately installed database.

Here are some options

  • thanks that’s what I’m talking about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.