当前位置: 技术问答>java相关
表单提交的问题。
来源: 互联网 发布时间:2017-04-25
本文导语: 我是通过jsp:setProperty方式的,可是在写bean是,一个表单文本就要对应一对set,get方法,这样太烦了,有没有好的一点的方法,把这些都抽象出来。 | import java.io.*; import java.sql.*; public class Dat...
我是通过jsp:setProperty方式的,可是在写bean是,一个表单文本就要对应一对set,get方法,这样太烦了,有没有好的一点的方法,把这些都抽象出来。
|
import java.io.*;
import java.sql.*;
public class DataBean{
Connection conn=null;
//初始化,连接数据库
public DataBean(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn= DriverManager.getConnection("jdbc:odbc:dsn1", "sa", "");
}
catch(Exception e){
}
}
//关闭连接
public void CloseConnection(){
try{
conn.close();
}
catch(Exception e){}
}
//添加记录
public String AddRecord(String TableName,String FieldName,String FieldValue){
String str="";
try{
String sql="insert into "+TableName+"("+FieldName+")values("+FieldValue+")";
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//更新记录
public String UpdateRecord(String TableName,String FieldStr,String WhereStr){
String str="";
try{
String sql="update "+TableName+" set "+FieldStr+" where "+WhereStr;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//删除记录
public String DeleteRecord(String TableName,String WhereStr){
String str="";
try{
String sql="delete from "+TableName+" where "+WhereStr;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//获取记录
public ResultSet GetRs(String TableName,String FieldName,String WhereStr,String OrderStr,String SortStr){
String sql = "";
String FieldName1="";
String SortStr1="";
ResultSet resultset=null;
try{
if(FieldName=="")
FieldName1="*";
else
FieldName1=FieldName;
if(SortStr!="desc")
SortStr1="asc";
else
SortStr1="desc";
sql="select "+FieldName1+" from "+TableName+" where "+WhereStr;
sql=sql+" Order by "+OrderStr+" "+SortStr1;
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
resultset = statement.executeQuery(sql);
}
catch(Exception exception){
resultset=null;
}
return resultset;
}
}
import java.sql.*;
public class DataBean{
Connection conn=null;
//初始化,连接数据库
public DataBean(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn= DriverManager.getConnection("jdbc:odbc:dsn1", "sa", "");
}
catch(Exception e){
}
}
//关闭连接
public void CloseConnection(){
try{
conn.close();
}
catch(Exception e){}
}
//添加记录
public String AddRecord(String TableName,String FieldName,String FieldValue){
String str="";
try{
String sql="insert into "+TableName+"("+FieldName+")values("+FieldValue+")";
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//更新记录
public String UpdateRecord(String TableName,String FieldStr,String WhereStr){
String str="";
try{
String sql="update "+TableName+" set "+FieldStr+" where "+WhereStr;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//删除记录
public String DeleteRecord(String TableName,String WhereStr){
String str="";
try{
String sql="delete from "+TableName+" where "+WhereStr;
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
str="OK";
}
catch(Exception e){
str= e.toString();
}
return str;
}
//获取记录
public ResultSet GetRs(String TableName,String FieldName,String WhereStr,String OrderStr,String SortStr){
String sql = "";
String FieldName1="";
String SortStr1="";
ResultSet resultset=null;
try{
if(FieldName=="")
FieldName1="*";
else
FieldName1=FieldName;
if(SortStr!="desc")
SortStr1="asc";
else
SortStr1="desc";
sql="select "+FieldName1+" from "+TableName+" where "+WhereStr;
sql=sql+" Order by "+OrderStr+" "+SortStr1;
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
resultset = statement.executeQuery(sql);
}
catch(Exception exception){
resultset=null;
}
return resultset;
}
}