Results 1 to 3 of 3
  1. #1
    tmukmkd is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default [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

    Java 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

    Java 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

    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
    Java Code:
    db.connect();
    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
    Java Code:
    sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
    it go directly to
    Java 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.
    Last edited by tmukmkd; 10-24-2010 at 11:12 AM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    I wonder if the database connect is failing,
    Java Code:
    if (!db.connect()) {
      // error, not able to connect to database, check url, db user, password, and grant in mysql database.
      return;
    }

  3. #3
    tmukmkd is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    thanks. it return an error. it have an error at dbcon.java
    Java Code:
    Class.forName(driverName);
    actually i have using this class when i make another program using struts. but as long as i remember it work.
    i will check the driver name.

    update: yes, my i update my jconnector to version 5.1 (my mysql is ver5.1) and add it into library. change the driver to "com.mysql.jdbc.Driver"

    and it done :D thanks.

Similar Threads

  1. How to retrive a string array data from a session
    By shandi in forum New To Java
    Replies: 1
    Last Post: 09-16-2010, 02:02 PM
  2. Retrive from DB
    By Dileep in forum JDBC
    Replies: 12
    Last Post: 09-25-2009, 09:58 AM
  3. Displaying data from mysql database.
    By Haresh gurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-09-2008, 02:54 AM
  4. how to call data from MySQL
    By rhlpnt in forum Java Servlet
    Replies: 0
    Last Post: 02-02-2008, 11:03 AM
  5. MySQL 5.0 data backup from java
    By sanjay_sharma77 in forum JDBC
    Replies: 1
    Last Post: 11-06-2007, 12:19 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •