1,105,633 Community Members

Retrieving database records using netbeans

Member Avatar
joe_ojah
Newbie Poster
4 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi everyone, i am new to netbeans. i am creating a system that can allocate rooms to students. My main problem is how to find out which rooms are full and which ones are available.
I am using Mysql and Java Netbeans. I have read some of the tutorials but i haven't fond out how i can do it.
does anyone know how i can do this in netbeans. Please help

Member Avatar
DeadSoul
Junior Poster in Training
56 posts since Dec 2009
Reputation Points: -2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
-1
 

i cant give you the code but i can tell you how to retrive data from database.
you can take help from the following example.

1.the user enter the table name on the web page the coding is given below

<html>
  <head>
    <title></title>
  </head>
  <body bgcolor="green">
   <center>
  <form  name="f1" action="http://localhost:8084/Database/TestJdbc" method="Post">
      Enter the table name : <input type="text" name="t1"  /><br>
       <input type="submit" value="OK" />
       </form>
       </center>
       </body>
</html>

2.the server side coding is given below

/*
 * TestJdbc.java
 *
 * Created on August 30, 2009, 8:26 PM
 */

import java.io.*;
import java.net.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author G@Me BuDDiE
 * @version
 */
public class TestJdbc extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        /* TODO output your page here*/
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet TestJdbc</title>");
        out.println("</head>");
        out.println("<body>");
        
        out.println("<h1>Servlet TestJdbc at " + request.getContextPath () + "</h1>");
          try{
             Connection conn;
             Statement stmt;
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             conn=DriverManager.getConnection("Jdbc:Odbc:SelectData");
             stmt=conn.createStatement();
             String s = request.getParameter("t1");
             ResultSet rs=stmt.executeQuery("select * from  " +s);
             out.println("<table border = 1>");
             while(rs.next())
             {
                 out.println("<tr><td>");
                 out.println(rs.getString(1)+"</td>");
                out.print("<td>");
                 out.println(rs.getString(2)+"</tr></td>");                 
             }
             out.println("</table>");
             
         }catch(Exception e){}
        out.println("</body>");
        out.println("</html>");
     
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

you have to create a database and store the database where your java file is stored which will be easier during coding.any more help you can ask.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
1
 

That is not a good example and it is a bad way to do things. Not to mention that the OP said nothing about html code.

Only the database connectivity is almost ok. You forgot to close the ResultSet, Statement and ResultSet.
First: How do you know that the table entered at the field will always have 2 columns.
Second: Never put html code in servlets. Read the data from the DB, put those into an array of objects using OOP, and then send the array of objects back to a jsp through the request in order to be displayed at the jsp page.

Of course the above would have been helpful if the OP has said that the whole application would be web application.

Member Avatar
nwilliams
Newbie Poster
3 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Of course, first you have to create the database and store it in the your Java file. You can develop MySQL backed applications with Netbaens and JAVA which might could give you the other Web services. It would be easier for you if you create the proper database so that you can allocate rooms to the students.

Member Avatar
ali.nazia999
Newbie Poster
1 post since May 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
                I want a code in which a jsp login page is calling a servlet and the server is connected to the again new jsp in which it shows the welcome page or it says that you are a valid user or it may be invalid user....plz ans as soon as possible, 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article