As we can see here you have closed connection to DB in that time when you try to setString do.
The way It is possible
- When you don’t create any connection to DB
- When you create but don’t open any connection to DB
- When you create and, even open, but, close connection before tries to make DML into DB.