weblogic 下 jsp 连接数据库的例(使用JDNI)

发表于:2007-07-01来源:作者:点击数: 标签:
%@ page contentType=text/html; charset=GBK % %@ page import= java . sql .*; page import=java.io.IOException , page import=javax.naming.Context , page import=javax.naming.InitialContext , page import=javax.sql.DataSource % %! java.sql.Connec
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*";
   page import="java.io.IOException ",
   page import="javax.naming.Context ",
   page import="javax.naming.InitialContext ",
   page import="javax.sql.DataSource " %>
   <%!
java.sql.Connection conn;
Statement stmt=null;
java.sql.ResultSet rs=null;
int flag=0;
javax.sql.DataSource ds=null;
public void jspInit(){
try{
javax.naming.InitialContext initx = new javax.naming.InitialContext();
  ds = (javax.sql.DataSource )initx.lookup("TestJndi") ;//JNDIName
  conn = ds.getConnection() ;
  }
catch(Exception ea){
    
}
}
   %>
<html>
<head>
<title>
Jsp1
</title>
</head>
<body>
<%
  stmt=conn.createStatement() ;  
  rs =stmt.executeQuery("select * from province") ;
   if (rs==null){
     out.print("rs is null");
   }
   while(rs.next() ){
     out.print(rs.getString("province_name")) ;
   }
%>
<h1>
JBuilder Generated JSP
</h1>
<form method="post">
<br>Enter new value   :  <input name="sample"><br>
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
<br>

</form>
</body>
</html>

原文转自:http://www.ltesting.net