当前位置: 技术问答>java相关
怎么看对数据库的添加、修改是否成功?急,在线给分!!
来源: 互联网 发布时间:2015-10-31
本文导语: 怎么看对数据库的添加、修改是否成功? | 最简单的方法是在数据库操作的bean里面定义一个String 用来返回结果: 这是我的bean的写法。 只要在页面调用getException()就可以知道操作是否成功,...
怎么看对数据库的添加、修改是否成功?
|
最简单的方法是在数据库操作的bean里面定义一个String 用来返回结果:
这是我的bean的写法。
只要在页面调用getException()就可以知道操作是否成功,我觉得这种做法是代价比较小的了。哈哈,欢迎大家交流
package sqlBean;
import java.sql .*;
import java.io.*;
public class sqlBean {
private String strip="";//"192.168.1.165";
private String strport;
private String strodbc;
private String struser="";//"sa";
private String strpassword="" ;
private String temp;
private int i;
private String exception="操作成功!";
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "";//"jdbc:odbc:biserver";
//getfile gf = new getfile();
//String sConnStr="";
Connection connect = null;
ResultSet rs = null;
public void forName(){
try {
getfile();
sConnStr="jdbc:odbc:"+strodbc;
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
exception=e.getMessage();
System.err.println( exception);
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
forName();
if(connect==null)
connect = DriverManager.getConnection(sConnStr,"sa","");
strpassword=strpassword.trim();
if (strpassword.equals(""))
{
connect = DriverManager.getConnection(sConnStr,struser,"");
}
else
{
connect = DriverManager.getConnection(sConnStr,struser,strpassword);
}
Statement stmt = connect.createStatement(rs.TYPE_SCROLL_SENSITIVE,rs.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
exception=ex.getMessage();
System.err.println(exception);
}
return rs;
}
public void updatesql(String sql) {
try {
forName();
strpassword=strpassword.trim();
if (strpassword.equals(""))
{
if(connect==null)
connect = DriverManager.getConnection(sConnStr,struser,"");
}
else
{
if(connect==null)
connect = DriverManager.getConnection(sConnStr,struser,strpassword);
}
Statement stmt = connect.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
exception=ex.getMessage();
System.err.println(exception);
}
}
public String getException(){
return exception;
}
这是我的bean的写法。
只要在页面调用getException()就可以知道操作是否成功,我觉得这种做法是代价比较小的了。哈哈,欢迎大家交流
package sqlBean;
import java.sql .*;
import java.io.*;
public class sqlBean {
private String strip="";//"192.168.1.165";
private String strport;
private String strodbc;
private String struser="";//"sa";
private String strpassword="" ;
private String temp;
private int i;
private String exception="操作成功!";
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "";//"jdbc:odbc:biserver";
//getfile gf = new getfile();
//String sConnStr="";
Connection connect = null;
ResultSet rs = null;
public void forName(){
try {
getfile();
sConnStr="jdbc:odbc:"+strodbc;
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
exception=e.getMessage();
System.err.println( exception);
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
forName();
if(connect==null)
connect = DriverManager.getConnection(sConnStr,"sa","");
strpassword=strpassword.trim();
if (strpassword.equals(""))
{
connect = DriverManager.getConnection(sConnStr,struser,"");
}
else
{
connect = DriverManager.getConnection(sConnStr,struser,strpassword);
}
Statement stmt = connect.createStatement(rs.TYPE_SCROLL_SENSITIVE,rs.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
exception=ex.getMessage();
System.err.println(exception);
}
return rs;
}
public void updatesql(String sql) {
try {
forName();
strpassword=strpassword.trim();
if (strpassword.equals(""))
{
if(connect==null)
connect = DriverManager.getConnection(sConnStr,struser,"");
}
else
{
if(connect==null)
connect = DriverManager.getConnection(sConnStr,struser,strpassword);
}
Statement stmt = connect.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
exception=ex.getMessage();
System.err.println(exception);
}
}
public String getException(){
return exception;
}
|
Statement Interface的executeUpdate() method会返回一个整数,如果是大于0表示操作成功,等于0表示没有满足条件的记录,如果抛出异常表示操作失败