当前位置: 技术问答>java相关
怎样从一个Class中取得ResultSet数据集
来源: 互联网 发布时间:2017-03-16
本文导语: 希望通过一个类来取得一数据库操作的数据集合,却不知怎样在外部取得这一集合.请高手指点 | package cgrs.robinson; import java.sql.*; import java.io.*; public abstract class sqlBean{ private String myDriver=...
希望通过一个类来取得一数据库操作的数据集合,却不知怎样在外部取得这一集合.请高手指点
|
package cgrs.robinson;
import java.sql.*;
import java.io.*;
public abstract class sqlBean{
private String myDriver="org.gjt.mm.mysql.Driver";
private String host="localhost";
private String database="forum";
private String user="";
private String pass="";
protected Connection myConn;
public sqlBean(){}
public void makeConnection() throws Exception{
Class.forName(myDriver);
myConn=DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,user,pass);
}
public abstract void cleanup() throws Exception;
public void makeDown() throws Exception{
cleanup();
myConn.close();
}
}
package cgrs.robinson;
import java.sql.*;
import java.io.*;
public class forumBean extends sqlBean{
ResultSet myResultSet=null;
Statement stmt=null;
public forumBean() {
super();
}
public boolean getNext() throws Exception{
return myResultSet.next();
}
public String getString(String fieldName) throws Exception{
return myResultSet.getString(fieldName);
}
public int getInteger(String fieldName) throws Exception{
return myResultSet.getInt(fieldName);
}
public java.util.Date getDate(String fieldName) throws Exception{
return myResultSet.getDate(fieldName);
}
public boolean ExecuteQuery(String strSql) throws Exception{
stmt= myConn.createStatement();
myResultSet=stmt.executeQuery(strSql);
return (myResultSet!=null);
}
public void ExecuteUpdate(String strSql) throws Exception{
stmt= myConn.createStatement();
stmt.executeUpdate(strSql);
}
public void cleanup() throws Exception{
stmt.close();
}
}
import java.sql.*;
import java.io.*;
public abstract class sqlBean{
private String myDriver="org.gjt.mm.mysql.Driver";
private String host="localhost";
private String database="forum";
private String user="";
private String pass="";
protected Connection myConn;
public sqlBean(){}
public void makeConnection() throws Exception{
Class.forName(myDriver);
myConn=DriverManager.getConnection("jdbc:mysql://"+host+"/"+database,user,pass);
}
public abstract void cleanup() throws Exception;
public void makeDown() throws Exception{
cleanup();
myConn.close();
}
}
package cgrs.robinson;
import java.sql.*;
import java.io.*;
public class forumBean extends sqlBean{
ResultSet myResultSet=null;
Statement stmt=null;
public forumBean() {
super();
}
public boolean getNext() throws Exception{
return myResultSet.next();
}
public String getString(String fieldName) throws Exception{
return myResultSet.getString(fieldName);
}
public int getInteger(String fieldName) throws Exception{
return myResultSet.getInt(fieldName);
}
public java.util.Date getDate(String fieldName) throws Exception{
return myResultSet.getDate(fieldName);
}
public boolean ExecuteQuery(String strSql) throws Exception{
stmt= myConn.createStatement();
myResultSet=stmt.executeQuery(strSql);
return (myResultSet!=null);
}
public void ExecuteUpdate(String strSql) throws Exception{
stmt= myConn.createStatement();
stmt.executeUpdate(strSql);
}
public void cleanup() throws Exception{
stmt.close();
}
}