servlet是万能的,你想要它做什么都可以,jsp编译后也是生成servlet,不过,一般来说用来做mvc的c层,就是控制层,用来调用不同的业务逻辑
JaveBean对应mvc的m层,就是数据层,负责根据业务逻辑处理数据
以上是我自己归纳的,意思应该差不多,呵呵
/**连库Bean
*最后修改日期:2007-9-5
*/
package com.deepteach.dao
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
public class DBjdbcConnection
{
//数据库名:deepteach_com 用户名:deepteach_com 密码:lvhaipeng
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"
//数据库
String sConnStr = "jdbc:microsoft:sqlserver://localhost:1433DatabaseName=deepteach_com"
//数据库用户名
String user = "deepteach_com"
//数据库用户密码
String password = "lvhaipeng"
Connection connect = null
ResultSet rs = null
int count = 0
public DBjdbcConnection()
{
try
{
Class.forName(sDBDriver)
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println( e.getMessage())
}
}
public ResultSet executeQuery(String sql)
{
rs = null
try
{
connect = DriverManager.getConnection(sConnStr,user,password)
Statement stmt = connect.createStatement()
rs = stmt.executeQuery(sql)
}
catch(SQLException ex)
{
System.err.println(ex.getMessage())
}
return rs
}
public int executeUpdate(String sql)
{
try
{
connect = DriverManager.getConnection(sConnStr,user,password)
Statement stmt = connect.createStatement()
count = stmt.executeUpdate(sql)
}
catch(SQLException ex)
{
System.err.println(ex.getMessage())
}
return count
}
}
JSP使用数据库的例子:showByJdbcOdbc.jsp
<%@ page contentType="text/htmlcharset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML><BODY bgcolor=cyan>
<% Connection con
Statement sql
ResultSet rs
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
}
catch(ClassNotFoundException e)
{ out.print(e)
}
try { con=DriverManager.getConnection("jdbc:odbc:mymoon","sa","sa")
sql=con.createStatement()
rs=sql.executeQuery("SELECT * FROM employee WHERE salary>3000")
out.print("<table border=2>")
out.print("<tr>")
out.print("<th width=100>"+"雇员号")
out.print("<th width=100>"+"姓名")
out.print("<th width=50>"+"出生日期")
out.print("<th width=50>"+"薪水")
out.print("</TR>")
while(rs.next())
{ out.print("<tr>")
out.print("<td >"+rs.getString(1)+"</td>")
out.print("<td >"+rs.getString(2)+"</td>")
out.print("<td >"+rs.getDate("birthday")+"</td>")
out.print("<td >"+rs.getFloat("salary")+"</td>")
out.print("</tr>")
}
out.print("</table>")
con.close()
}
catch(SQLException e)
{ out.print(e)
}
%>
</BODY></HTML>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)