JDBC Connection Code for MYSQL/ORACLE DB

Standard

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class dataBaseConnManager {
static Connection conn = null;
static Statement stmt = null;
constant cnst = new constant();
public dataBaseConnManager() {
try {
if(conn==null || stmt == null || stmt.isClosed() == true)
{
conn = null;
stmt = null;
constant cnst = new constant();
// JDBC driver name and database URL

final String JDBC_DRIVER = “com.mysql.jdbc.Driver”; //FOR MYSQL

//final String JDBC_DRIVER = “oracle.jdbc.driver.OracleDriver”; //FOR ORACLE

final String DB_URL = “jdbc:mysql://localhost/”+cnst.dataBaseName; //FOR MYSQL

//final String DB_URL = “jdbc:oracle:thin:@129.146.79.106:1521:orcl”; //FOR ORACLE

final String USER = cnst.dataBaseUserName;
final String PASS = cnst.dataBasePassword;
try{
//STEP 2: Register JDBC driver
Class.forName(“com.mysql.jdbc.Driver”); //FOR MYSQL

//Class.forName(“oracle.jdbc.driver.OracleDriver”); //FOR ORACLE

//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
stmt = conn.createStatement();

}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Install Java, mySQL, apacheTomcat on Linux Server & Open Specific Port Using FireWalld

Standard
  • Install Java
    • Download and upload any jdk say “jdk-8u162-linux-x64.rpm” file at any directory say “/tmp”
    • cd /usr/java
    • sudo rpm -Uvh /tmp/jdk-8u162-linux-x64.rpm

 

 

 

  • Open Specific Port Using FireWalld
    • [root@centos7 ~]# firewall-cmd –permanent –add-port=100/tcp
      success
    • [root@centos7 ~]# firewall-cmd –reload
      success
    • We can check the ports that are opened in the current default zone with ‘–list-ports’.
      • [root@centos7 ~]# firewall-cmd –list-ports

100/tcp

MySQL Connection Limit Issue resolve

Standard

Follow following steps :

  1. vi /etc/my.cnf
  2. max_connections=2800
  3. save file
  4. service mysqld stop
  5. service mysqld start
  6. login into mysql and hit following command to check max connection limit :
    1. show variables like ‘max_connections’;
    2. Check “Threads_connected” variable to see total connections in use with following query :
      • show status like ‘%onn%’;

 

Solution in java code to not to increase MySQL connections :

  • In JDBC connection code strict to connection variable to not to create if already exists :

static Connection conn = null;
static Statement stmt = null;
constant cnst = new constant();
public dataBaseConnManager() {
if(conn==null)
{
conn = null;
stmt = null;
constant cnst = new constant();
// JDBC driver name and database URL
final String JDBC_DRIVER = “com.mysql.jdbc.Driver”;
final String DB_URL = “jdbc:mysql://localhost/”+cnst.dataBaseName;
final String USER = cnst.dataBaseUserName;
final String PASS = cnst.dataBasePassword;
try{
//STEP 2: Register JDBC driver
Class.forName(“com.mysql.jdbc.Driver”);
//STEP 3: Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
stmt = conn.createStatement();

}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}

Change mysql root password on Centos7

Standard
1. Stop mysql:
systemctl stop mysqld

2. Set the mySQL environment option 
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

3. Start mysql usig the options you just set
systemctl start mysqld

4. Login as root
mysql -u root

5. Update the root user password with these mysql commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword')
    -> WHERE User = 'root' AND Host = 'localhost';
mysql> FLUSH PRIVILEGES;
mysql> quit

*** Edit ***
As mentioned my shokulei in the comments, for 5.7.6 and later, you should use 
   mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
Or you'll get a warning

6. Stop mysql
systemctl stop mysqld

7. Unset the mySQL envitroment option so it starts normally next time
systemctl unset-environment MYSQLD_OPTS

8. Start mysql normally:
systemctl start mysqld

Try to login using your new password:
7. mysql -u root -p