View: 3465|Reply: 2
|
solve [java][mysql] data tak boleh panggil/edit/delete
[Copy link]
|
|
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
- 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;
- }
- }
Copy the Code
ni login.jsp
- <%@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>
Copy the Code
ni login.java. module untuk login.jsp
- 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();
- }
- }
Copy the Code
problemnya bila aku debug, sampai ke
tak de masalah. lancar. so maknanya database dapat dijalankan kan?
tapi masa sampai ke
- sq = db.selectQuery("*", "user", "usernm='" + username + "' and userpw='" + password + "'");
Copy the Code
terus turun ke
- catch (Exception e) {
- 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. |
|
|
|
|
|
|
|
dah selesai. memang hubungan dengan database tak betul dari awal.
kesan guna kod ni- if (!db.connect()) {
- System.out.println("dbcon error");
- return;
- }
Copy the Code sebagai ganti kepada koddan 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. |
|
|
|
|
|
|
|
nampak best la pulak bila belajar pograming. aku basic, html, php dengan java scrip tau ler. |
|
|
|
|
|
|
| |
|