关于用javabean插入数据库的问题
我想用javabean同时运行两条插入语句,这是我的代码<%@pageimport="java.util.*"%><%@pageimport="java.io.*"%><...
我想用javabean同时运行两条插入语句,这是我的代码
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="myjsp.DatabaseBean"%>
<jsp:useBean id="redBean" scope="session" class="myjsp.DatabaseBean"/>
<jsp:useBean id="redBean1" scope="session" class="myjsp.DatabaseBean"/>
<HTML>
<HEAD><TITLE>Completion of Update</TITLE></HEAD>
<BODY>
<%
redBean.connectDB("Book","","");
String user = request.getParameter("username");
String pass = request.getParameter("password");
String fname = request.getParameter("First_Name");
String lname = request.getParameter("Last_Name");
String email = request.getParameter("Email");
String pet = request.getParameter("pet");
String petname = request.getParameter("petname");
String type = "Customer";
//update
redBean.updateDB("INSERT INTO Customer(custuser, custpass, First_Name, Last_Name, Email , pet , petname) VALUES ('"+user+"', '"+pass+"', '"+fname+"', '"+lname+"', '"+email+"', '"+pet+"', '"+petname+"' )");
redBean.closeDB();
redBean1.connectDB("Book","","");
redBean1.updateDB("INSERT INTO Customer(INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
%>
</BODY>
</HTML>
我这么写第二个插入语句无效,第一个可以插入
这个是我的bean
package myjsp;
import java.util.*;
import java.io.*;
import java.sql.*;
public class DatabaseBean
{
Connection connection;
Statement statement;
ResultSet columns;
public DatabaseBean(){
}
public void connectDB(String dataSName,String uName,String pwd)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:"+dataSName,uName,pwd);
statement = connection.createStatement();
}
catch(Exception ex){ System.out.println(ex.toString()); }
}
public ResultSet queryDB(String sqlStatement)
{
try{columns=statement.executeQuery(sqlStatement);
}
catch(Exception ex){
System.out.println(ex.toString());
}
return columns;
}
public void closeDB()
{
try{
statement.close();
connection.close();
}
catch(Exception ex){System.out.println(ex.toString());}
}
public int updateDB(String sqlStatement)
{
int status=0;
try{
status=statement.executeUpdate(sqlStatement);
}
catch(Exception ex){System.out.println(ex.toString());
}
return status;
}
}
求教各位! 展开
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="myjsp.DatabaseBean"%>
<jsp:useBean id="redBean" scope="session" class="myjsp.DatabaseBean"/>
<jsp:useBean id="redBean1" scope="session" class="myjsp.DatabaseBean"/>
<HTML>
<HEAD><TITLE>Completion of Update</TITLE></HEAD>
<BODY>
<%
redBean.connectDB("Book","","");
String user = request.getParameter("username");
String pass = request.getParameter("password");
String fname = request.getParameter("First_Name");
String lname = request.getParameter("Last_Name");
String email = request.getParameter("Email");
String pet = request.getParameter("pet");
String petname = request.getParameter("petname");
String type = "Customer";
//update
redBean.updateDB("INSERT INTO Customer(custuser, custpass, First_Name, Last_Name, Email , pet , petname) VALUES ('"+user+"', '"+pass+"', '"+fname+"', '"+lname+"', '"+email+"', '"+pet+"', '"+petname+"' )");
redBean.closeDB();
redBean1.connectDB("Book","","");
redBean1.updateDB("INSERT INTO Customer(INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
%>
</BODY>
</HTML>
我这么写第二个插入语句无效,第一个可以插入
这个是我的bean
package myjsp;
import java.util.*;
import java.io.*;
import java.sql.*;
public class DatabaseBean
{
Connection connection;
Statement statement;
ResultSet columns;
public DatabaseBean(){
}
public void connectDB(String dataSName,String uName,String pwd)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:"+dataSName,uName,pwd);
statement = connection.createStatement();
}
catch(Exception ex){ System.out.println(ex.toString()); }
}
public ResultSet queryDB(String sqlStatement)
{
try{columns=statement.executeQuery(sqlStatement);
}
catch(Exception ex){
System.out.println(ex.toString());
}
return columns;
}
public void closeDB()
{
try{
statement.close();
connection.close();
}
catch(Exception ex){System.out.println(ex.toString());}
}
public int updateDB(String sqlStatement)
{
int status=0;
try{
status=statement.executeUpdate(sqlStatement);
}
catch(Exception ex){System.out.println(ex.toString());
}
return status;
}
}
求教各位! 展开
2个回答
展开全部
xc_jetguo 说得对,SQL语法有问题。
看看你这两个SQL吧!第一条是正确的,第二条你就按一条的写法写不就对了。而你这里是不一样的。
redBean.updateDB("INSERT INTO Customer(custuser, custpass, First_Name, Last_Name, Email , pet , petname) VALUES ('"+user+"', '"+pass+"', '"+fname+"', '"+lname+"', '"+email+"', '"+pet+"', '"+petname+"' )");
redBean1.updateDB("INSERT INTO Customer(INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
仔细比较一下,把后面那一条改成这样就行了。
redBean1.updateDB("INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
看看你这两个SQL吧!第一条是正确的,第二条你就按一条的写法写不就对了。而你这里是不一样的。
redBean.updateDB("INSERT INTO Customer(custuser, custpass, First_Name, Last_Name, Email , pet , petname) VALUES ('"+user+"', '"+pass+"', '"+fname+"', '"+lname+"', '"+email+"', '"+pet+"', '"+petname+"' )");
redBean1.updateDB("INSERT INTO Customer(INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
仔细比较一下,把后面那一条改成这样就行了。
redBean1.updateDB("INSERT INTO Users(username, password , Type) VALUES ('"+user+"', '"+pass+"', '"+type+"')");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询