CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 3465|Reply: 2

solve [java][mysql] data tak boleh panggil/edit/delete

[Copy link]
Post time 23-10-2010 11:41 AM | Show all posts |Read mode
Post Last Edit by kmkd at 24-10-2010 18:11

aku tak pasti kenapa masalah ni berlaku, aku bagi contoh login la ek.

ni dbcon.java. gunanya adalah untuk buat connection/kerja2 dengan database
  1. package sitoo;
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;

  7. public class dbcon {

  8.     String url = "jdbc:mysql://localhost:3306/";
  9.     String dbName = "sitoo";
  10.     String driverName = "com.mysql.jdbc.Driver";
  11.     String dbuser = "root";
  12.     String dbpass = "";
  13.     Connection con = null;
  14.     Statement stmt = null;

  15.     public dbcon() {
  16.     }

  17.     /**
  18.      * open the database connection
  19.      * @param dbName to set the database name value
  20.      * @param userName to set the username value
  21.      * @param password to set the password value
  22.      * @return true if connect. else return exception status and false.
  23.      */
  24.     public boolean connect() {

  25.         try {
  26.             Class.forName(driverName);
  27.             con = DriverManager.getConnection(url + dbName, dbuser, dbpass);
  28.             stmt = con.createStatement();

  29.         } catch (SQLException se) {
  30.             se.printStackTrace();
  31.             return false;
  32.         } catch (ClassNotFoundException ce) {
  33.             ce.printStackTrace();
  34.             return false;
  35.         }
  36.         return true;
  37.     }

  38.     /**
  39.      * close the database connection
  40.      * @return true if disconnect. else return exception status and false.
  41.      */
  42.     public boolean disconnect() {
  43.         try {
  44.             stmt.close();
  45.             con.close();
  46.         } catch (SQLException se) {
  47.             se.printStackTrace();
  48.             return false;
  49.         }
  50.         return true;
  51.     }

  52.     public int executeStatement(String spWhat) {
  53.         int i = 0;
  54.         try {
  55.             i = stmt.executeUpdate(spWhat);
  56.         } catch (SQLException se) {
  57.             se.printStackTrace();
  58.             return -1;
  59.         }
  60.         return i;
  61.     }

  62.     public ResultSet selectQuery(String fields, String tableName, String cond) {
  63.         ResultSet resultSet = null;
  64.         try {
  65.             resultSet = stmt.executeQuery("SELECT " + fields + " FROM " + tableName + " WHERE " + cond);
  66.         } catch (SQLException se) {
  67.             se.printStackTrace();
  68.         }
  69.         return resultSet;
  70.     }
  71. }
Copy the Code


ni login.jsp
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3.    "http://www.w3.org/TR/html4/loose.dtd">

  4. <html>
  5.     <head>
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7.         <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
  8.         <title>JSP Page</title>
  9.     </head>
  10.     <body>
  11.         <div id="login">
  12.             <form name="login" action="login.sitoo">
  13.                 <input type="text" name="username" value="username" size="12" />
  14.                 <input type="password" name="password" value="password" size="12" />
  15.                 <input type="submit" name="submit" />
  16.             </form>
  17.         </div>
  18.     </body>
  19. </html>
Copy the Code


ni login.java. module untuk login.jsp
  1. package sitoo;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import java.sql.*;
  12. import java.util.*;

  13. public class login extends HttpServlet {

  14.     String username;
  15.     private String password;
  16.     String role;
  17.     private String temp;
  18.     private String status;
  19.     //ResultSet sq = null;

  20.     protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
  21.         response.setContentType("text/html");
  22.         PrintWriter out = response.getWriter();
  23.         password = request.getParameter("password");
  24.         username = request.getParameter("username");

  25.         try {
  26.             dbcon db = new dbcon(); //make static and nonstatic function can be connect.
  27.             db.connect();
  28.             ResultSet sq;
  29.             sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
  30.             

  31.             if (sq.next()) {
  32.                 temp = sq.getString("userpw");
  33.                 role = sq.getString("userrl");
  34.                 status = sq.getString("userst");

  35. //system logik kat sini

  36.             }
  37.             return;

  38.         } catch (Exception e) {
  39.             e.printStackTrace();
  40.         }
  41.     }
Copy the Code


problemnya bila aku debug, sampai ke
  1. db.connect();
Copy the Code

tak de masalah. lancar. so maknanya database dapat dijalankan kan?
tapi masa sampai ke
  1. sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
Copy the Code

terus turun ke
  1. catch (Exception e) {
  2.             e.printStackTrace();
Copy the Code


dalam debug result kata null pointer exception.

ada idea tak? sebab benda ni buat semua fungsi yang terlibat dengan database tak boleh jalan. tapi kalau guna kat mysql query browser/administrator boleh buat kerja2 dengan baik. so maknanya database ok kan?
mula2 aku fikir stored procedure aku yang salah tapi bila guna sintak yang paling basic sekali pun tak boleh juga memang pelik.
Reply

Use magic Report


ADVERTISEMENT


 Author| Post time 24-10-2010 06:11 PM | Show all posts
dah selesai. memang hubungan dengan database tak betul dari awal.
kesan guna kod ni
  1. if (!db.connect()) {
  2.   System.out.println("dbcon error");
  3.   return;
  4. }
Copy the Code
sebagai ganti kepada kod
  1. db.connect();
Copy the Code
dan akan keluar ralat pada konsol.

so bila check balik Jconnector yang aku guna adalah 5.0 manakala database aku adalah 5.1.x
tambahkan jconnector yang baru kepada library, jalan. ok. masalah selesai.
Reply

Use magic Report

Post time 13-12-2010 03:52 PM | Show all posts
nampak best la pulak bila belajar pograming. aku basic, html, php dengan java scrip tau ler.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT



 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

11-12-2024 02:13 AM GMT+8 , Processed in 0.044857 second(s), 16 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list