Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DB;
- import java.sql.*;
- public class DatabaseConnection {
- private static final DatabaseConnection _instance = new DatabaseConnection( );
- public static DatabaseConnection getInstance()
- {
- return _instance;
- }
- private static final String DatabaseName = "test";
- private static final String DatabaseUsername = "root";
- private static final String DatabasePassword = "101010";
- private static final String DatabaseHost = "localhost";
- private Connection _databaseConnection;
- private DatabaseConnection()
- {
- // String connectionUrl = String.format( "jdbc:mysql:///%s", DatabaseName );
- String connectionUrl = String.format( "jdbc:mysql://%s/%s", DatabaseHost, DatabaseName );
- try {
- _databaseConnection = DriverManager.getConnection( connectionUrl, DatabaseUsername, DatabasePassword );
- }
- catch ( SQLException e ) {
- e.printStackTrace( );
- }
- }
- public Connection getConnection()
- {
- return _databaseConnection;
- }
- public ResultSet executeQuery( String query )
- {
- try {
- return _databaseConnection.createStatement( ).executeQuery( query );
- }
- catch ( SQLException e ) {
- e.printStackTrace( );
- return null;
- }
- }
- public int executeUpdate( String query )
- {
- try {
- return _databaseConnection.createStatement( ).executeUpdate( query );
- }
- catch ( SQLException e ) {
- e.printStackTrace( );
- return -1;
- }
- }
- public static void main(String[] args) throws SQLException {
- String id = null;
- String nume = null;
- DatabaseConnection.getInstance().getConnection();
- ResultSet re =getInstance().executeQuery("SELECT * FROM `test`.`clients`;");
- while (re.next())
- try{
- id = re.getString("idClients");
- nume = re.getString("Name");
- }
- catch (SQLException e ) {
- e.printStackTrace( );
- }
- System.out.println("id="+id+" nume="+nume);
- }
- }
Add Comment
Please, Sign In to add comment