I have tried to build a web application using JAVA.
This is the code of connection between JDBC and MySQL server. The code did not run.
import java.sql.*;
public class RegisterDao {
private String dburl = "jdbc:mysql://localhost:3306/hospitalm";
private String dbuname = "root";
private String dbpassword = "1234";
private String dbdriver = "com.mysql.jdbc.Driver";
public void loadDriver(String dbDriver) {
try {
Class.forName(dbDriver);
} catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection(dburl, dbuname, dbpassword);
System.out.println("Connection was established");
} catch(SQLException e){
e.printStackTrace();
}
return con;
}
public String insert(Member member) {
loadDriver(dbdriver);
Connection con = getConnection();
String result = "data entered successfully";
String sql = "insert into hospitalm.patient values(?, ?, ?, ?, ?)";
try {
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, member.getPid());
ps.setString(2, member.getName());
ps.setString(3, member.getAddress());
ps.setString(4, member.getEmail());
ps.setString(5, member.getType());
ps.executeUpdate();
} catch (SQLException e){
e.printStackTrace();
result = "Data not entered successfully";
}
return result;
}
}
Can anyone let me know what is the error on the above code?
The SQL table is like:
CREATE TABLE patient(
pid VARCHAR(10),
pname VARCHAR(40) NOT NULL,
address VARCHAR(10),
email_id VARCHAR(20) NOT NULL,
ptype VARCHAR(2),
PRMARY KEY(pid),
CHECK (email_id like '%_@__%.__%'),
CHECK (ptype in ('G','S'))
);
I got HTTP Status 500 Internal Server error.
Type: Exception Report
Message: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "con" is null
Description: The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception:
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "con" is null RegisterDao.insert(RegisterDao.java:47) Register.doPost(Register.java:47) javax.servlet.http.HttpServlet.service(HttpServlet.java:681) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
The servlet code that I added:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String Pid = request.getParameter("pid");
String Name = request.getParameter("patientname");
String Address = request.getParameter("patientaddress");
String Email = request.getParameter("email");
String Type = request.getParameter("patienttype");
Member member = new Member(Pid, Name, Address, Email, Type);
RegisterDao rDao= new RegisterDao();
String result = rDao.insert(member);
response.getWriter().print(result);
//doGet(request, response);
}
}
What are the possible causes of getting this type of error?