JSP calls the stored procedure of SQL Server 2000

xiaoxiao2021-03-06  16

Create a stored procedure:

Use mydata;

Create Proc Proc_members_1 asselect * from membrate where id betWeen 1000 and 1005return

Package test;

Import java.sql. *;

public class SqlBean {private Connection conn; private Statement stmt; private ResultSet rs; private CallableStatement cstmt;. private synchronized Connection getConnection () throws SQLException {try {Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver") newInstance ( ); String url = "JDBC: Microsoft: SQLServer: // localhost: 1433; databasename = mydata"; connection conn = drivermanager.getConnection (URL, "SA", "); Return Conn; Catch; CauseNOTFoundExcection E) { e.printStackTrace (); throw new SQLException (e.getMessage ());} catch (InstantiationException e) {e.printStackTrace (); throw new SQLException (e.getMessage ());} catch (IllegalAccessException e) {e. PRINTSTACKTRACE (); throw new sqlexception (E.GetMessage ());}}} public resultset DocallableSelect (String SQL) throws sqlexception {cstmt = conn.preparecal l (sql); return cstmt.executeQuery ();} public SqlBean () throws SQLException {conn = getConnection (); stmt = conn.createStatement ();} public ResultSet doSelect (String sql) throws SQLException {ResultSet rs = stmt. executeQuery (sql); return rs;} public boolean doUpdate (String sql) throws SQLException {int i = stmt.executeUpdate (sql); return 0 = i;!!} public void close () throws SQLException {if (rs = null rs.close (); if (stmt! = null) stmt.close (); if (conn! = null) conn.close ();}}

RS = SQLBean.docalLableSelect ("EXECUTE PROC_MEMBERS_1"); while (rs.next ()) {INT ID = rs.getinT ("ID"); string name = rs.getstring ("name"); string sex = rs. GetString ("sex"); long age = rs.getlong ("age"); string createtime = rs.getstring ("createtime"); out.println ("

" ID "," Name " , " SEX ", " Age ", " CreateTime " ");} SQLBean.close (); Result:

1000, MEMBER_994, ML, 994, 2005-03-14 17: 44: 57.217

1001, MEMBER_995, FM, 995, 2005-03-14 17: 44: 57.217

1002, MEMBER_996, ML, 996, 2005-03-14 17: 44: 57.217

1003, MEMBER_997, FM, 997, 2005-03-14 17: 44: 57.217

1004, MEMBER_998, ML, 998, 2005-03-14 17: 44: 57.217

1005, MEMBER_999, FM, 999, 2005-03-14 17: 44: 57.217

转载请注明原文地址:https://www.9cbs.com/read-45741.html

New Post(0)