Before you killing me, I know about the:
http://www.daniweb.com/forums/thread141776.html , but for the moment, I DON"T have the time.

Secondly, I'm new to Java platform and JSP.

I'm trying to make a simple application, but I don't get it.

I'm using Intellij 8, Tomcat 6, MySQL

I have a java class which makes the connection with the database and a jsp file, but is not working.

The java file:

package com.packages;

import java.sql.*;

public class conexiune
{
    String URL = "jdbc:mysql://localhost:3306/agenda";
    String user = "root";
    String pass = "root";
    Connection con;
    Statement stat;
    PreparedStatement pre;
    ResultSet res;


	public void creeaza()
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(URL, user, pass);
		}
		catch(Exception ex)
        {
            
        }
	}

	public void inchide()
	{
		try
		{
			con.close();
		}
		catch(Exception ex)
        {

        }
	}
 

	public ResultSet interogheaza(String sql)
	{
		try
		{
			stat = con.createStatement();
			res = stat.executeQuery(sql);
			return res;
		}
		catch(Exception ex)
		{
			return null;
		}
	}

	public ResultSet interogheazaPrepared(String Sql, String Sql2)
	{
		try
		{
			pre = con.prepareStatement(Sql);
            pre.setString(1, Sql2);
            res = pre.executeQuery ();
            return res;
		}
		catch(Exception ex)
		{
			return null;
		}
	}
}

The JSP file:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="conex" scope="page" class="com.packages.conexiune" />

<html>
    <head>
        <title>
            Lista contactelor
        </title>

        <SCRIPT LANGUAGE="JavaScript">

        </SCRIPT>
    </head>

    <body>



        <form name="form" action="index.jsp" method="get">
            Cautare
            <input name="cauta" type="text">
            <input type="submit" name="nume" value="Nume">
            <input type="submit" name="oras" value="Oras">
        </form>
        <form name="form" action="afisare.jsp" method="get">


            <table border="0" cellspacing=0 width="100%">
                <tr>
                    <th>Lista contactelor</th>

                </tr>
            </table>
            <br/>

            <table border="1" cellspacing=0 width="100%">
                <tr>
                    <th>Nr. crt.</th>
                    <th>Nume</th>
                    <th>Prenume</th>
                    <th>Adresa</th>
                    <th>Zip</th>
                    <th>Oras</th>
                    <th>Telefon</th>
                    <th>Civila</th>
                    <th>Nr. copii</th>
                    <th>Adresa mail</th>
                </tr>


                <%
                
                    conex.creeaza();
                    int i = 1;
                    ResultSet res;
                    res = conex.interogheaza("SELECT * from adresa");
                    while (res.next())
                    {
                %>
                <tr>

                    <td align="center"> <input type="radio" name="punct" checked="checked" value="<%=res.getString("id") %>"><%=i %></td>
                    <td align="center"><%=res.getString("Nume") %></td>
                    <td align="center"><%=res.getString("Prenume")%></td>
                    <td align="center"><%=res.getString("Adresa")%></td>
                    <td align="center"><%=res.getString("Cod_Zip")%></td>
                    <td align="center"><%=res.getString("Oras")%></td>
                    <td align="center"><%=res.getString("Nr_telefon")%></td>
                    <td align="center"><%=res.getString("Stare_civila")%></td>
                    <td align="center"><%=res.getString("Nr_copii")%></td>
                    <td align="center"><%=res.getString("Adresa_Mail")%></td>
                    <%
                        i++; 
                     }
                    %>
                </tr>

            </table>

            <br/>
            <input type="submit" name="adaug"   value="Adauga">
            <input type="submit" name="modific" value="Modifica" >
            <input type="submit" name="sterg"   value="Sterge" onclick="return confirm('Doriti stergerea persoanei selectate?');" />

        </form>  </body>
</html>

When I'm trying to run the app, I receive this error:

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 65

62: int i = 1;
63: ResultSet res;
64: res = conex.interogheaza("SELECT * from adresa");
65: while (res.next())
66: {
67: %>
68: <tr>

i dont know mysql..

but with toad. you have to open toad for oracle. then click on tnsnames editor. then click on orcl.

there is something called host= something, port="number" and servicename=something

"jdbc:mysql://something:number/something";


you have to replace those. plus also the user pass you have to put the username and password for the database. how do you enter your sql? you type a username and password. you have to put thoser two.

There are quite a few things I would like to tell you in this post:-

  • Firstly never ever use the root account in mysql, apart from for performing administrative tasks.
  • Telling that you do not have the time for following Good Design (and following MVC) is like telling you don't have the time to do any research before spending millions on buying a lavish new sports car, eventually you are going to regret it.
  • Your catch blocks are not only for show, or just to stop the compiler from complaining of uncaught exceptions, they are there so that you handle those exceptional situations by writing contingency plans there or at least print a simple stack trace of the exception so that you can signal yourself or the rest of the program of problems like not able to establish a database connection etc.
  • Next is follow consistent indentation, if not for anyone else, it will ultimately aid you in reading your own code.
  • Do not return a ResultSet directly from a function, a ResultSet requires that its associated database connection be open throughout the time the ResultSet is needed, Use a CachedRowSet in case you have to return a ResultSet, or the best approach would be what Peter followed in the MVC thread.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.