JSP & MySQL Tutorial

JSP stands for JavaServer Pages, which is Sun's solution for creating dynamic Web pages. JSP provides sufficient server side scripting support for creating database-related Web applications. It works on all mainstream browsers, including IE, Firefox, Google Chrome, Opera and Safari, and with HTML and CSS to develop dynamic websites, adding interactivity to your Web pages. MySQL is a popular open-source database management system. You can connect to MySQL database with JSP. The database stores data for your website, and JSP can retrieve and display the data.

Instructions

    • 1

      Log into your PHPMyAdmin as the root user. Create a new database for your website such as "forum" in the text box under "Create new database." Click "Create." Create a new table "user" by typing a name for the table. Specify the number of fields. Click "Go" to create the table.

    • 2

      Create two fields for the new table, named "id" and "name." Select "varchar" as their data types.

    • 3

      Open your desired Web page in the program that you used to create it. Enter the following code in the body section to connect to your database via JSP backend:

      <% page language="java" import="java.sql.*" %>

      <%

      Connection cn=null;

      Statement st=null;

      ResultSet ex=null;

      Java.sql.Connection mysql_cn

      Class.forName ("org.gjt.mm.mysql.Driver");

      cn=DriverManager.getConnection ("jdbc:mysql://localhost:3306/forum? User=<user>&password=<password>";

      %>

      Java is a strongly typed language, which means its variables need to be declared before use. These defined variables are related with the Java.sql; "cn" is a Connection type variable that holds the connection type object. The ResultSet variable "ex" holds the retrieved results, while the Statement variable "st" contains the SQL statement.

    • 4

      Retrieve and display the data of table "user" via the following code:

      st=con.createStatement();

      ex=st.executeQuery ("select * from user");

Related Searches:

References

Comments

Related Ads

Featured