0

When I try to connect my servlet through my mysql database I got error. I can successful load and register my driver but can not connect to database.

I used Class.forName("com.mysql.cj.jdbc.Driver"); to load and register driver. There is an error of

Severe:   java.sql.SQLNonTransientConnectionException at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at Data_Enter.service(Data_Enter.java:22)
public class Data_Enter extends HttpServlet {
    public void service(HttpServletRequest req, HttpServletResponse res)
             throws ServletException, IOException{
        Connection con = null;
        Statement stmt = null;
        res.setContentType("text/html;charset=UTF-8");
         PrintWriter out = res.getWriter();
         String usurname = req.getParameter("t1");
         String uname = req.getParameter("t2");

         try {
               Class.forName("com.mysql.cj.jdbc.Driver");
                 System.out.println("Connected");
                 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/billing_system", "root", "");
                 System.out.println("eroor");
                stmt=con.createStatement();
                 stmt.executeUpdate("insert into store_data values('"+usurname+"'.'"+uname+"')");
                 System.out.println("Data inserted");
             } catch (ClassNotFoundException ex) {
                 System.err.println("try again");
             } catch (SQLException ex) {
            Logger.getLogger(Data_Enter.class.getName()).log(Level.SEVERE, null, ex);
        }

Database name: billing_system Table name:store_data

What should i do next to solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • you should google that exception name, you should check to which line in your code it points and see if you notice something there – Stultuske Jun 14 '21 at 09:03
  • Which version of Java and, in particular, JDBC are you using? This might not resolve your issue, but bear in mind that for JDBC 4.0+ you don't need to invoke `Class.forName(...)` anymore, because all the available will be automatically loaded by JVM. – Marco Tizzano Jun 14 '21 at 09:17
  • @Stultuske, yes it stuck at this line "con = DriverManager.getConnection("jdbc:mysql://localhost:3306/billing_system", "root", ""); ". yeah i did but no relevant answer. :( – Divya42 Jun 14 '21 at 11:47
  • @MarcoTizzano, How can i check JDBC version? – Divya42 Jun 14 '21 at 11:51

0 Answers0