当前位置:编程学习 > JSP >>

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

答案:<%@ 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>

上一个:Weblogic 下调试servlet
下一个:jsp调用Entity Bean的例子

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,