0

This is my simple code to test connectivity with MySQL :

import java.sql.*;
import java.util.*;

public class PreparedStatementTest {

    public static void main(String[] args) throws Exception {
        String[] str = {"ram", "shyam", "radhe", "lakhan"};
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:shubh", "sa", "shubham");
        PreparedStatement ps = con.prepareStatement("insert into shubham_table values(?,?)");
        for (int i = 0; i < 4; i++) {
            ps.setInt(1, i);
            ps.setString(2, str[i]);
            ps.executeUpdate();
        }
        PreparedStatement prs = con.prepareStatement("select *from shubham_table where id=?");
        for (int i = 0; i < 4; i++) {
            prs.setInt(1, i);
            ResultSet rs = prs.executeQuery();
            while (rs.next()) {
                System.out.print("id = " + rs.getInt(1));
                System.out.println("name = " + rs.getString(2));

            }
        }
        rs.close();
        prs.close();
        ps.close();
        con.close();
    }
}

This code is working properly and updating my already existing table in database but when I try to create a connection in my web app on Apache it throws ClassNotFoundException. The source code in my application is

import javax.servlet.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class RegFormServlet implements Servlet {

    public void init(ServletConfig sc) throws ServletException {

        System.out.println("created");
    }

    public ServletConfig getServletConfig() {
        return null;
    }

    public void service(ServletRequest req, ServletResponse res) throws
            ServletException, IOException {

        System.out.println("before con   mysql");
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:shubh", "sa", "shubham");

            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String address = req.getParameter("address");

            Statement st = con.createStatement();
            String ddl =
            "create table shubham_table3 (name varchar(30),e-mail 
          varchar(20)
            ,address varchar
            (100))";
       st.execute(ddl);

            PreparedStatement ps = con.prepareStatement(
            "insert into shubham_table 
        values( ?,  ?,  ?)
            ");
        ps.setString(1, name);
            ps.setString(2, email);
            ps.setString(3, address);
            PrintWriter out = res.getWriter();
            out.println("You Are Registered Successfully yeah!!!!");
            st.close();
            ps.close();
            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    public String getServletInfo() {
        return null;
    }

    public void destroy() {

    }
}

How can I fix this?

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Shubham Tyagi
  • 181
  • 1
  • 3
  • 14

1 Answers1

0

You are using Type1 jdbc driver to connect to Mysql database. Please check Tomcat and its jdk version. From Jdk8 onwards Type1 driver is not allowed to use.

Alternatively you can use Mysql Type4 driver to do operation. Here is link

Once downloaded mysql connector driver add it to /WEB-INF/lib folder.

Hopefully It should solve issue.

Gaurav J
  • 76
  • 3