jsp页面怎么对数据库进行增加操作?是不是每个jsp页面都得写连接数据库的代码?jsp页面通过JavaBean连接数据库行不?
The value for the useBean class attribute src/java/source/PayDA is invalid.是什么意思呢?
The value for the useBean class attribute src/java/source/PayDA is invalid.是什么意思呢?
解决方案 »
- 这句代码什么意思<a href="index.jsp?page=<%=1%>">第一页</a>
- 简历的CRM的项目描述这样可以吗
- "会者不难,难者不会"的面试题
- SQL语句正确但是RESULTSET中没有值
- 怎样把SQL语句的结果导入到文件中?
- 怎么获取javascript表单参数?
- 服务器是win2000server,客户端是win2000 advanced server,问题是客户端通过ie访问bs程序出错
- 如何将jsp生成安装程序
- 三大框架问题
- tomcat部署项目到外网服务器后,内网可以访问,外网不能访问
- eclipse项目内好多文件都报404错误,之前好好的
- 为什么点击连接后不能打开服务器上含有中文的文件?
对数据的操作写到另一个类,例如 UserDao.javajsp页面对数据的操作最好提交到控制层去,例如Servlet。
这样显示层、控制层和数据链路层就分开了,也就是MVC模式。
src/java/source/PayDA is invalid 这个资源非法了。
package myBean;
import java.io.*;import java.sql.*;
public class SqlBean
{
private String DBDriver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/yao?user=root&password=123456&characterEncoding=gbk";
private String uid="root";
private String pwd="123456";
public Connection conn=null;
public ResultSet rs=null;
public ResultSet query(String mySql) throws Exception
{
try
{
Class.forName(DBDriver).newInstance();
conn=DriverManager.getConnection(url,uid,pwd);
Statement stmt=conn.createStatement();
stmt.execute("use yao;");
rs=stmt.executeQuery(mySql); return rs;
}
catch (SQLException e)
{
System.out.println("这里查询异常!");
}
return null;
} public void update(String mySql)throws Exception
{
try
{
Class.forName(DBDriver).newInstance();
conn=DriverManager.getConnection(url,uid,pwd);
Statement stmt=conn.createStatement();
stmt.execute("use yao;");
stmt.executeUpdate(mySql);
stmt.close();
conn.close();
}
catch (SQLException e)
{
System.out.println("222222查询异常!");
}
}
public void setDBDriver(String DBDriver)
{
this.DBDriver=DBDriver;
}
public String getDBDriver()
{
return this.DBDriver;
}
public void setUrl(String url)
{
this.url=url;
}
public String getUrl()
{
return this.url;
}
public void setUid(String uid)
{
this.uid=uid;
}
public String getUid()
{
return this.uid;
}
public void setPwd(String pwd)
{
this.pwd=pwd;
}
public String getPwd()
{
return this.pwd;
}
}
public class BaseDAO {
private static String DRIVER="oracle.jdbc.driver.OracleDriver";
private static String URL="jdbc:oracle:thin:@localhost:1521:accp";
private static String USER="epet";
private static String PWD="epet";
public static Connection getCon(){
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL,USER,PWD);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public List<Object> executeQuery(Class obj,String sql,Object...args){
Connection con=getCon();
ResultSet rs=null;
PreparedStatement pst=null;
List<Object> objectList=new ArrayList<Object>();
try {
pst=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pst.setObject(i+1, args[i]);
}
rs=pst.executeQuery();
while(rs.next()){
Object o=obj.newInstance();
Method[] methods=obj.getDeclaredMethods();
for (Method method : methods) {
String methodName=method.getName();
if(methodName.startsWith("set")){
Object value=rs.getObject(methodName.replace("set",""));
if(value!=null){
if(value instanceof BigDecimal){
value=((BigDecimal)value).intValue();
method.invoke(o, value);
}
if(value instanceof String){
value=value.toString();
method.invoke(o, value);
}
if(value instanceof Date){
value=value.toString();
method.invoke(o, value);
}
}
}
}
objectList.add(o);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(rs,pst,con);
}
return objectList;
}
public int executeSQL(String sql,Object...args){
Connection con=getCon();
int result=0;
PreparedStatement pst=null;
try {
pst=con.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pst.setObject(i+1, args[i]);
}
result=pst.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(null,pst,con);
}
return result;
}
public void closeAll(ResultSet rs,Statement st,Connection con){
try {
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(con!=null){
con.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}