Real-field JSP advanced programming One

Source: Internet
Author: User
Tags stmt

Many JSP beginners in learning simple JSP programming, often stay in the JSP inside the SQL statement to tune a JavaBean database connection stage, stalled.

This simple tutorial is expected to help beginners learn to use OOP ideas for JSP programming.

Scenario: A simple news system that consists of 2-3 data tables.
The database system uses MySQL, and of course it is similar to others.
First datasheet, and the main data sheet: News

CREATE TABLE news2 (NewSID int NOT NULL,
UserID int,
Kwid int,//key word foreign key
Title varchar (100),
Content text,
Hits int,
CDate VARCHAR2 (30),
Mdate VARCHAR2 (30),
Primary KEY (NewSID));

Insert one more sample data:

Insert into News2 (NewSID, title, content) VALUES (1, ' Test title ', ' Test body ');


Design idea: Programming with MVC pattern, packing data in a helper class News.java,
And through the Newsdao.java database operation.
In the design phase, UML is used to sketch the object of the system.
... omitted here

The main methods of Newsdao are:
1. Public News getnewsbyprimarykey (int newsid);
2. Public news[] Getrecentnews ();
3. Public news[] Gethotnews ();
......

The code for News.java is as follows:

Package news;

public class News {
private int newsid;
private int userid;
private int kwid;
private int hits;
Private String title;
Private String content;
Private String CDate;
Private String mdate;

Public News () {}
Public News (int newsid,int userid,int kwid,int hits,string title,string content,string)
{
This.newsid=newsid;
This.userid=userid;
This.kwid=kwid;
This.hits=hits;
This.title=title;
This.content=content;
This.cdate=cdate;
}

public News (int ID, string t, string cnt) {
This.newsid = ID;
This.title = t;
This.content = CNT;
}
public int Getnewsid ()
{
return newsid;
}
public void Setnewsid (int newsid)
{
This.newsid=newsid;
}


public int GetUserID ()
{
return userid;
}
public void Setuserid (int userid)
{
This.userid=userid;
}

public int Getkwid ()
{
return kwid;
}
public void Setkwid (int kwid)
{
This.kwid=kwid;
}

public int gethits ()
{
return hits;
}
public void sethits (int hits)
{
This.hits=hits;
}

Public String GetTitle ()
{
return title;
}
public void Settitle (String title)
{
This.title=title;
}

Public String getcontent ()
{
return content;
}
public void SetContent (String content)
{
This.content=content;
}


Public String getcdate ()
{
return cdate;
}
public void Setcdate (String cdate)
{
This.cdate=cdate;
}

}

Description: This program can be used as JavaBean, as a parameter carrier for entry form (params Holder).

The most important file Newsdao.java code is as follows:

Package news;

Import java.sql.*;

public class Newsdao
{

Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url= "Jdbc:mysql://localhost:3306/joke?user=root";


Public Newsdao ()
{
try {
Class.forName ("Com.mysql.jdbc.Driver");
}
catch (Java.lang.ClassNotFoundException e) {
System.err.println ("Joke ():" +e.getmessage ());
}
}

Public News getnewsbyprimarykey (int newsid) throws SQLException
{
Connection Conn=null;
Statement stmt;
ResultSet rs;
News = null;

String sql= "Select Newsid,title,content from News2" +
"Where newsid=" +newsid+ "";
conn = getconnection ();
stmt = Conn.createstatement ();
Rs=stmt.executequery (SQL);

if (Rs.next ())
{
News = new News (Rs.getint (1), rs.getstring (2), rs.getstring (3));
}
Rs.close ();
Stmt.close ();
Conn.close ();
return news;
}

Private Connection getconnection () throws SQLException
{
Connection conn = null;
conn = drivermanager.getconnection (URL);
Return conn;
}

}

Description: This program as a sample code, very simple, not consider the exception, the more important method
such as Getrecentnews (), we can refer to the implementation of their own.

Simple JSP Call test program: getnews.jsp

<% @page contenttype= "text/html;charset=gb2312"%>
<% @page import= "news.*"%>
<%
Newsdao Newsdao = new Newsdao ();
News news = Newsdao.getnewsbyprimarykey (1);
if (news!= null) {
Out.println ("Title:" +news.gettitle ());
Out.println ("<br>");
Out.println ("Body:" +news.getcontent ());
}
else Out.println ("Failed.");
%>

Description: This simplified implementation is actually a DAO mode of omission, there should be Interface,dao factory.

If you have time, you may give examples later, of course, when you are familiar with OOP methods, you can also make up for yourself.

Also, compile the time with Javac News/*.java is OK.

If the system prompts you not to find news, it is actually because your newsdao.java has a problem, not really news is not on the path. In the same package, it is generally possible to compile this way. It's just that the wrong hints are misleading.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.