Examples of calling JavaBean using Java application and JSP respectively

Source: Internet
Author: User
Tags getmessage odbc sql string stmt
Application|js//java Bean
File Name:faq.java
Import java.sql.*;

public class FAQ {
String sdbdriver = "Sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "Jdbc:odbc:faq";
Connection conn = null;
ResultSet rs = null;

Public FAQ () {
try {
Class.forName (Sdbdriver);
}catch (Java.lang.ClassNotFoundException e) {
System.err.println ("FAQ ():" + e.getmessage ());
}
}

Public ResultSet executequery (String sql) {
rs = null;
try {
conn = Drivermanager.getconnection (SCONNSTR);
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (SQL);
}catch (SQLException ex) {
System.err.println ("Aq.executequery:" + ex.getmessage ());
}
Return RS;
}
}

Java application
File Name:testfaq.java
Import java.sql.*;
Class Getrs
{
public static String Answer;
public static String Subject;
public static string[][] Rsarray = new string[9][2];;
public static int n = 0;
Getrs () {
try {
FAQ workm = new FAQ ();
ResultSet oRs = Workm.executequery ("SELECT * from FAQ");
System.out.println (Ors.getcount ());
while (Ors.next ()) {
System.out.println (ors.getstring ("Answer"));
System.out.println (ors.getstring ("Subject"));
n=n+1;
}
}catch (SQLException ex)
{
System.out.println ("Error:" +ex);
}
}
}

public class Testfaq {
public static void Main (string[] args) {
Getrs AA = new Getrs ();
}
}

Jsp
File name:testfaq.jsp

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> My FAQ!</title>
<body>
<p><b> this is my faq!</b></p>.
<%@ page contenttype= "mimetype; charset=gb2312 "%>
<%@ page language= "java" import= "java.sql.*"%>

<%
FAQ workm = new FAQ ();
ResultSet RS = Workm.executequery ("SELECT * from FAQ");
String tt;
while (Rs.next ()) {
tt = rs.getstring ("Answer");
Out.print ("<LI>" + rs.getstring ("Subject") + "</LI>");
Out.print ("<pre>" + TT + "</pre>");
}
Rs.close ();
%>
</body>


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.