📄 jdbcconpoolservlet.java
字号:
import java.io.*;
import java.sql.*;
import javax.sql.DataSource;
import javax.servlet.http.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* A connection to SQL Server2000 through Weblogic JDBC Conn Pool
*/
public class JdbcConPoolServlet extends HttpServlet{
javax.sql.DataSource ds;
public void init(){
try{
//Create a connection to the WebLogic Server JNDI Naming Service:
Context ctx=new InitialContext();
//Create a new DataSource by Locating it in the Naming Service:
ds=(javax.sql.DataSource)ctx.lookup("jdbcsample-dataSource");
}catch(Exception e){
System.out.println("Init Error: "+e);
}
}
public void service(HttpServletRequest req,HttpServletResponse resp)
throws IOException{
Connection myConn=null;
Statement stmt=null;
ResultSet rs=null;
try{
PrintWriter out=resp.getWriter();
out.println("<html>");
out.println("<head><title>JdbcConPoolServlet</title></head>");
out.println("<body>");
System.out.println("start conn");
//Create a new JDBC connection from the DataSource:
myConn=ds.getConnection();
System.out.println("conn succ");
//Create a instance of the java.sql.Statement class
//and use the factory method called createStatment()
//available in the Conenction class to create a new statment.
stmt=myConn.createStatement();
//System.out.println("stmt succ");
rs=stmt.executeQuery("select * from t_employee");
//System.out.println("executeQuery succ");
while(rs.next()){
out.println(rs.getString("emp_no")+"<br>");
out.println(rs.getString("emp_name")+"<br>");
out.println(rs.getString("emp_age")+"<br>");
out.println(rs.getString("salary")+"<br>");
out.println(rs.getString("comment")+"<p>");
}
rs.close();
stmt.close();
out.println("</body></html>");
}catch(Exception e){
System.out.println("Service Error: "+e);
}finally{
if(rs!=null){
try{
rs.close();
}catch(Exception ignore){
}
}
if(stmt!=null){
try{
stmt.close();
}catch(Exception ignore){
}
}
if(myConn!=null){
try{
myConn.close();
}catch(Exception ignore){
}
}
}//end finally
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -