I am trying to connect my Java application to a MySQL database and used the following lines of code:
import java.sql.*;
public class AcceptValues extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = "jdbc:mysql://localhost:3306/db_name";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,userName,password);
out.print("Connection estd");
Statement st = conn.createStatement();
st.executeQuery("insert into table_name values(2, 'testing');");
}
conn.close();
} catch (Exception e) {
out.print("Error : " +e.getMessage());
}
}
}
I also set the class name to mysql-connector-java-5.1.29-bin.jar, which I downloaded from mysql site. But I still cannot connect to the database using the above lines of code and throws exeption com.mysql.jdbc.Driver as an error.
I am new to java development and any help would be greatly appreciated. Thank.
source
share