Skip to main content
deleted 13 characters in body
Source Link
Sleiman Jneidi
  • 3.2k
  • 11
  • 20

You could use a Pooling Data Source to reuselimit the same connection if there is annumber of opened oneconnections.

   InitialContext ctx = new InitialContext();
   DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
   Connection connection = null;
   try {
        connection = ds.getConnection();
       //...
   }finally{
    try{
     if(null != connection){
     connection.close();
     }
    }catch(Exception ex){ // use a more specific exception
      // logging
    }
   }

Moreover, use Prepared Statement instead of Statements, it gives automatic prevention of SQL injection attacks.

Use proper logging instead of printStackTrace

You could use a Pooling Data Source to reuse the same connection if there is an opened one.

   InitialContext ctx = new InitialContext();
   DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
   Connection connection = null;
   try {
        connection = ds.getConnection();
       //...
   }finally{
    try{
     if(null != connection){
     connection.close();
     }
    }catch(Exception ex){ // use a more specific exception
      // logging
    }
   }

Moreover, use Prepared Statement instead of Statements, it gives automatic prevention of SQL injection attacks.

Use proper logging instead of printStackTrace

You could use a Pooling Data Source to limit the number of opened connections.

   InitialContext ctx = new InitialContext();
   DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
   Connection connection = null;
   try {
        connection = ds.getConnection();
       //...
   }finally{
    try{
     if(null != connection){
     connection.close();
     }
    }catch(Exception ex){ // use a more specific exception
      // logging
    }
   }

Moreover, use Prepared Statement instead of Statements, it gives automatic prevention of SQL injection attacks.

Use proper logging instead of printStackTrace

Source Link
Sleiman Jneidi
  • 3.2k
  • 11
  • 20

You could use a Pooling Data Source to reuse the same connection if there is an opened one.

   InitialContext ctx = new InitialContext();
   DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
   Connection connection = null;
   try {
        connection = ds.getConnection();
       //...
   }finally{
    try{
     if(null != connection){
     connection.close();
     }
    }catch(Exception ex){ // use a more specific exception
      // logging
    }
   }

Moreover, use Prepared Statement instead of Statements, it gives automatic prevention of SQL injection attacks.

Use proper logging instead of printStackTrace