Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to use the javabeans

i am a newbie of jsp. i have a problem with the javabeans i create for a jsp page.Plz help me.
This is the javabean
package author;

import java.sql.*;
import java.util.*;

public class Books{
String error;
Connection con;

public Books() {}

public void connect() throws ClassNotFoundException, SQLException, Exception
{
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost/wrox");
}
catch (ClassNotFoundException cnfe)
{
error="ClassNotFoundException : Could not locate DB driver";
throw new ClassNotFoundException(error);
}
catch (SQLException cnfe)
{
error="SQLException : Could not connect to database";
throw new SQLException(error);
}
catch (Exception e)
{
error="Exception : An unknown error occurred while connecting to database";
throw new Exception(error);
}
}

public void disconnect() throws SQLException
{
try
{
if(con!=null)
{
con.close();
}
}
catch (SQLException sqle)
{
error=("SQLException : Unable to close the database connection");
throw new SQLException(error);
}
}

public void addBooks(int id, String title, float price, int cid) throws SQLException, Exception
{
if(con!=null)
{
try
{
PreparedStatement updatebooks;
updatebooks = con.prepareStatement("insert into Book values(?, ?, ?, ?);");
updatebooks.setInt(1, id);
updatebooks.setString(2, title);
updatebooks.setFloat(3, price);
updatebooks.setInt(4, cid);
updatebooks.execute();
}
catch (SQLException sqle)
{
error="SQLException : update failed, possible duplicate entry";
throw new SQLException(error);
}
}
else
{
error="Exception : Connection to database is lost";
throw new Exception(error);
}
}

}
This the the jsp page that use the javabean
<%@ page language="java"
import="java.sql.*, java.io.*, java.util.*
%>



Add Books


<%
int id=Integer.parseInt(request.getParameter("Title_ID"));
string title=request.getParameter("Title");
float price=Float.parseFloat(request.getParameter("Price"));
int cid=Integer.parseInt(request.getParameter("Category_ID"));
book.connect();
book.addBooks(id, title, price, cid);
book.disconnect();
%>

The new book has been added

Click HereTo Go Back


I cannot add the values to the database(mysql database).
Plz help me
thankz

Comments

  • treshrtreshr Member Posts: 326
    First of all I wouldn't use my JavaBean like this.
    You're combining two completely different things in one JavaBean and you're destroying the OOP in this way.

    What I would do if I were you is to create a seperate Connection bean and a Books bean.

    If you're formiliar with the MVC (Model View Controller) concept, i won't explain it again, if you're not create your app like this.

    Make a model, that is your Books bean. That holds all the values and can write to the variables and has all the methods neede for that.

    Make a view, that's your form using HTML. And let it send to the controller.

    Make a controller, that checks if you want to add a book to a database, remove it or change it.
    In the controller is a method that checks for that.
    An example:
    A method that checks if a specific parameter says that you want to add a book.
    The controller goes to the specific method, calls the bean for the connection, and makes a connection, gets all the parameters and writes the specific query in the database.

    Use the Books bean to let the user check if the info is right. So if he send the info, write the info in the bean and let another page get the info. This way you'll have a good architecture and you'll always be able to expand it with another part of your software.

    If you have any more questions, just ask.

    Hope I helped you enouph.



    --=][tReShR][=--

Sign In or Register to comment.