jsp调用存储过程
答案:3 悬赏:80 手机版
解决时间 2021-04-04 14:56
- 提问者网友:ミ烙印ゝ
- 2021-04-03 21:01
jsp调用存储过程
最佳答案
- 五星知识达人网友:举杯邀酒敬孤独
- 2021-04-03 21:15
jsp调用存储过程的实例
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
String driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new UnavailableException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("create database bookstore");
stmt.executeUpdate("use bookstore");
stmt.executeUpdate("create table bookinfo(id INT not null primary key,title VARCHAr(50) not null,author VARCHAr(50) not null,bookconcern VARCHAr(100) not null,publish_date DATE not null,price FLOAT(4,2) not null,amount SMALLINT,remark VARCHAr(200)) ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'Java从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");
stmt.addBatch("insert into bookinfo values(2,'JSP深入编程','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");
stmt.executeBatch();
PrintWriter out=resp.getWriter();
out.println("success!");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init() throws ServletException
{
String driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new UnavailableException("加载数据库驱动失败!");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try
{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("create database bookstore");
stmt.executeUpdate("use bookstore");
stmt.executeUpdate("create table bookinfo(id INT not null primary key,title VARCHAr(50) not null,author VARCHAr(50) not null,bookconcern VARCHAr(100) not null,publish_date DATE not null,price FLOAT(4,2) not null,amount SMALLINT,remark VARCHAr(200)) ENGINE=InnoDB");
stmt.addBatch("insert into bookinfo values(1,'Java从入门到精通','张三','张三出版社','2004-6-1',34.00,35,null)");
stmt.addBatch("insert into bookinfo values(2,'JSP深入编程','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");
stmt.executeBatch();
PrintWriter out=resp.getWriter();
out.println("success!");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
全部回答
- 1楼网友:想偏头吻你
- 2021-04-03 22:26
CallableStatement cstmt = null;
Connection conn = getConnection();//得到连接
cstmt = conn.prepareCall("{call 储存过程名称(?,?,?,?,?,?)}");//有几个参数就几个问号
cstmt.setString(1,"值");//第一个参数,参数是什么类型就SET什么类型
cstmt.setString(2,"值");//第二个参数
...
cstmt.setString(n,"值");//第N个参数
- 2楼网友:走死在岁月里
- 2021-04-03 21:41
你好。
1
callablestatement cstmt=myconn.preparecall("{call get_employee()}");
如果调用的存储不成不需要参数的话,那()可以不写,即call get_employee。
2
root cause javax.servlet.servletexception: com.microsoft.sqlserver.jdbc.sqlserverdriver
root cause java.lang.classnotfoundexception: com.microsoft.sqlserver.jdbc.sqlserverdriver
数据库连接的驱动包没有加载吧。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯