《JSP中使用数据库》由会员分享,可在线阅读,更多相关《JSP中使用数据库(32页珍藏版)》请在金锄头文库上搜索。
1、 JSP中使用数据库例子1showByJdbcOdbc.jsp 3000); out.print(); out.print(); out.print(+雇员号); out.print(+姓名); out.print(+出生日期); out.print(+薪水); out.print(); while(rs.next() out.print(); out.print(+rs.getString(1)+); out.print(+rs.getString(2)+); out.print(+rs.getDate(birthday)+); out.print(+rs.getFloat(salary)+
2、); out.print() ; out.print(); con.close(); catch(SQLException e) out.print(e); %例子2showBySQLDriver.jsp 3000); out.print(); out.print(); out.print(+雇员号); out.print(+姓名); out.print(+出生日期); out.print(+薪水); out.print(); while(rs.next() out.print(); out.print(+rs.getString(1)+); out.print(+rs.getString(2
3、)+); out.print(+rs.getDate(birthday)+); out.print(+rs.getFloat(salary)+); out.print() ; out.print(); con.close(); catch(SQLException e) out.print(e); %例子3QueryBean.javapackage database.operation;import java.sql.*;public class QueryBean String databaseName=; /数据库名 String tableName=; /表名 String user=
4、; /用户 String secret= ; /密码 StringBuffer queryResult; /查询结果 public QueryBean() queryResult=new StringBuffer(); try Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver); catch(Exception e) public void setDatabaseName(String s) databaseName=s.trim(); queryResult=new StringBuffer(); public String
5、 getDatabaseName() return databaseName; public void setTableName(String s) tableName=s.trim(); queryResult=new StringBuffer(); public String getTableName() return tableName; public void setSecret(String s) secret=s.trim(); queryResult=new StringBuffer(); public String getSecret() return secret; publ
6、ic void setUser(String s) user=s.trim(); queryResult=new StringBuffer(); public String getUser() return user; public StringBuffer getQueryResult() Connection con; Statement sql; ResultSet rs; try queryResult.append(); String uri= jdbc:sqlserver:/127.0.0.1:1433;DatabaseName=+databaseName; String id=u
7、ser; String password=secret; con=DriverManager.getConnection(uri,id,password); DatabaseMetaData metadata=con.getMetaData(); ResultSet rs1=metadata.getColumns(null,null,tableName,null); int 字段个数=0; queryResult.append(); while(rs1.next() 字段个数+; String clumnName=rs1.getString(4); queryResult.append(+clumnName+); queryResult.append(); sql=con.createStatement(); rs=sql.executeQuery(SELECT * FROM +tableName); while(rs.next() queryResult.append(); for(int k=1;k=字段个数;k+)