JSP调用JAVA类

钟跃民 发布于 2012/02/10 11:36
阅读 5K+
收藏 0

我开始是把所有代码都写在JSP里面的

功能什么的都可以完成

现在我想把他们分开写

那位有时间的朋友帮我了解下

这个技术叫什么?怎么完成。

这里是一个简单的JSP代码:

<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*" errorPage=""%>

<html>
  	<head>
		<title>调用java连接数据库test</title>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	</head>
	
<body bgcolor="#ffffff">
	<div align="center"><font color="#000000" size="5">调用java连接数据库test </font> </div>
<table width="75%" border="1" cellspacing="1" cellpadding="1" align="center">

<%
String sql = sql1; 
sql1 = "SELECT * FROM LeaveMessage";
%>
<%@ include file="DB.java" %>

  	<tr>
		<td width=12% align=center>姓名</td>
		<td width=12% align=center>性别</td>
		<td width=12% align=center>年龄</td>
		<td width=52% align=center>留言</td>
  	</tr>

	<tr>
    <td width=12% align=center><%out.print(Username);%></td>
		<td width=12% align=center>	<%out.print(Sex);%></td>
		<td width=12% align=center><%out.print(Age);%></td>
		<td width=52% align=center><%out.print(Matter);%></td>
	</tr>

</table>
</body>
</html>

这里是上面JSP要调用的JAVA类:

package com.db;

import java.sql.*;

public class DB {
	
	//String[]argument = new String[5];
	String Id = "";
	String Username = "";
	String Sex = "";
	String Age = "";
	String Matter = "";

		public void DBArgument(String getsql){
			String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
			String url = "jdbc:odbc:Northwind";
			String username = "";
			String password = "";
			String thissql = getsql;
			DBconnect(driver,
					  url,
					  username,
					  password,
					  thissql);
		}

		public void DBconnect(String driver,
							  String url,
							  String username,
							  String password,
							  String sql){
			try{
				Class.forName(driver);
				Connection connection = DriverManager.getConnection(url,username,password);
				Statement statement = connection.createStatement();
				ResultSet resultSet = statement.executeQuery(sql);		
					while(resultSet.next()){
						Id = resultSet.getString("Id");
						Username = resultSet.getString("Username");
						Sex = resultSet.getString("Sex");
						Age = resultSet.getString("Age");
						Matter = resultSet.getString("Matter");
/*						argument[0]= Id;
						argument[1]= Username;
						argument[2]= Sex;
						argument[3]= Age;
						argument[4]= Matter;
*/					
					}
			resultSet.close();
			statement.close();
			connection.close();
			}
			catch(ClassNotFoundException cnfe){
				System.err.println("Error loading driver:" + cnfe);	
			}
			catch(SQLException sqle){
				System.err.println("Error with connection:" + sqle);
			}
			//return argument;		
		}
		
		public void doall(){
			String getsql="";
			DBArgument(getsql);
		}

		public static void main(String[] args) {
			DB lets = new DB();
			lets.doall();
		}
	}
		 

 

我想让这个JSP给.java传一个String sql的参数

然后运行这个.java 得到得到返回值  或者直接调用里面的变量 并显示到界面上

可能我写的根本就驴唇不对马嘴  希望朋友们能耐心的帮帮我

加载中
0
乱世键客
乱世键客
用现有MVC framework或者自己开发Servlet+Action+DAO
0
yyp
yyp
oschina这有现成的,红薯大哥都共享了。
0
p
petter
<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*;DB" errorPage=""%>

 

<html>
      <head>
        <title>调用java连接数据库test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
   
<body bgcolor="#ffffff">
    <div align="center"><font color="#000000" size="5">调用java连接数据库test </font> </div>
<table width="75%" border="1" cellspacing="1" cellpadding="1" align="center">

<%
String sql = sql1;

sql1 = "SELECT * FROM LeaveMessage";

DB db=new DB();

DBArgument(sql1);

%>

      <tr>
        <td width=12% align=center>姓名</td>
        <td width=12% align=center>性别</td>
        <td width=12% align=center>年龄</td>
        <td width=52% align=center>留言</td>
      </tr>

    <tr>
    <td width=12% align=center><%out.print(db.userName);%></td>
        <td width=12% align=center>    <%out.print(db.Sex);%></td>
        <td width=12% align=center><%out.print(db.Age);%></td>
        <td width=52% align=center><%out.print(db.Matter);%></td>
    </tr>

</table>
</body>
</html>

0
Ryan-瑞恩
Ryan-瑞恩

引用来自“petter”的答案

<%@ page contentType="text/html;charset=GB2312" language="java" import="java.sql.*;DB" errorPage=""%>

 

<html>
      <head>
        <title>调用java连接数据库test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
   
<body bgcolor="#ffffff">
    <div align="center"><font color="#000000" size="5">调用java连接数据库test </font> </div>
<table width="75%" border="1" cellspacing="1" cellpadding="1" align="center">

<%
String sql = sql1;

sql1 = "SELECT * FROM LeaveMessage";

DB db=new DB();

DBArgument(sql1);

%>

      <tr>
        <td width=12% align=center>姓名</td>
        <td width=12% align=center>性别</td>
        <td width=12% align=center>年龄</td>
        <td width=52% align=center>留言</td>
      </tr>

    <tr>
    <td width=12% align=center><%out.print(db.userName);%></td>
        <td width=12% align=center>    <%out.print(db.Sex);%></td>
        <td width=12% align=center><%out.print(db.Age);%></td>
        <td width=52% align=center><%out.print(db.Matter);%></td>
    </tr>

</table>
</body>
</html>

页面最后不要出现数据库操作方面的东西。
0
佐岸
佐岸

你真够猛的。。。你适合去做PHP开发。。。。

难道你们都没有要求过代码规范么?

返回顶部
顶部