[solve] cannot retrive data from mysql
i have a problem with my database, when im make a query using mysql query browser it's works like charm but when i query it from java it have a problem
this is my dbcon.java. to connent the module to the database
Code:
package sitoo;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dbcon {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "sitoo";
String driverName = "com.mysql.jdbc.Driver";
String dbuser = "root";
String dbpass = "";
Connection con = null;
Statement stmt = null;
public dbcon() {
}
/**
* open the database connection
* @param dbName to set the database name value
* @param userName to set the username value
* @param password to set the password value
* @return true if connect. else return exception status and false.
*/
public boolean connect() {
try {
Class.forName(driverName);
con = DriverManager.getConnection(url + dbName, dbuser, dbpass);
stmt = con.createStatement();
} catch (SQLException se) {
se.printStackTrace();
return false;
} catch (ClassNotFoundException ce) {
ce.printStackTrace();
return false;
}
return true;
}
/**
* close the database connection
* @return true if disconnect. else return exception status and false.
*/
public boolean disconnect() {
try {
stmt.close();
con.close();
} catch (SQLException se) {
se.printStackTrace();
return false;
}
return true;
}
public int executeStatement(String spWhat) {
int i = 0;
try {
i = stmt.executeUpdate(spWhat);
} catch (SQLException se) {
se.printStackTrace();
return -1;
}
return i;
}
public ResultSet selectQuery(String fields, String tableName, String cond) {
ResultSet resultSet = null;
try {
resultSet = stmt.executeQuery("SELECT " + fields + " FROM " + tableName + " WHERE " + cond);
} catch (SQLException se) {
se.printStackTrace();
}
return resultSet;
}
}
this is my login.jsp
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
<title>JSP Page</title>
</head>
<body>
<div id="login">
<form name="login" action="login.sitoo">
<input type="text" name="username" value="username" size="12" />
<input type="password" name="password" value="password" size="12" />
<input type="submit" name="submit" />
</form>
</div>
</body>
</html>
this is login.java
Code:
package sitoo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.*;
import java.util.*;
public class login extends HttpServlet {
String username;
private String password;
String role;
private String temp;
private String status;
//ResultSet sq = null;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
password = request.getParameter("password");
username = request.getParameter("username");
try {
dbcon db = new dbcon(); //make static and nonstatic function can be connect.
db.connect();
ResultSet sq;
sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
if (sq.next()) {
temp = sq.getString("userpw");
role = sq.getString("userrl");
status = sq.getString("userst");
//system logik kat sini
}
return;
} catch (Exception e) {
e.printStackTrace();
}
}
i try to debug it and when it goes to
in login.java it pass it. which means the connection to the database is correct izzit? if i have problem with database it will give the error from this line already. but i've pass it.
but when it goes to
Code:
sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
it go directly to
Code:
catch (Exception e) {
e.printStackTrace();
}
with error nullpointerexception
anyone have any idea to overcome this problem? thanks :)
note: i don't know either i need to put this on jsp forum or database forum.