Use the JavaBean operation database table and its subsections
I wrote a javabean when I was a third time, and I can implement the connection database, the table's operation, and the field value of the table, and extract new SQL conditions after the string value, I will now take it out with you.
dbBean.java// connected database SQL Server2000 package pub; import java.sql *;. import java.io *;. import java.util *;. public class dbBean {public Connection conn = null; public Statement stmt = null; public ResultSet RS = null; public dbbean () {Try {class.forname ("sun.jdbc.odbc.jdbcodbcdriver");} catch (classnotfoundexception e) {system.err.println ("OPENDB ():" E.getMessage ());}} public ResultSet executeQuery (String sql) {try {conn = DriverManager.getConnection ( "jdbc: odbc: KMHXBY", "sa", ""); stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet. Concur_updatable; rs = stmt.executeQuery (SQL); // Execute SQL} catch (java.sql.sqlexception e) {system.err.println ("OpenDb.executeQuery ():" E.getMessage ());} Return RS;} public int executeUpdate (string sql) {int ROWSCOUNT = 0; try {conn = drivermanager.getConnection ("JDBC: ODB c: foxv21 "," sa "," "); stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rowscount = stmt.executeUpdate (sql); // execute sql} catch (java.sql.SQLException e ) {System.err.println ("OpenDb.executeUpdate (): E.GetMessage ());} Return RowsCount;} public void destroy (); conn.close (); Catch (Exception E) {System.err.Println ("DBOPERATE.DESTROY ():" E.GetMessage ());}}} getsql.java // Operation FOA_WEBPURVIEW table, there are two fields,
A f_username, a F_SearchPurview, this label If the condition of the record user query, then I have to add the necessary conditions, form a real SQL statement, which is a string operation.
Package stringop; import java.lang. *; import java.util. *; import java.sql. *; import pub. *; public class getsql {public string {public string {public string {public string {public String RSAR; File: // file file must meet the condition of the policy PUBLIC STRING rsfile; file: // Ordinary file must meet the condition of public string ql = ""; // read SQL statement public string rsaddar = ""; file: // file additional condition PUBLIC STRING RSADDFILE = ""; File: // General Additional Conditions Public ResultSet DBRS; Public GetSQL (String S) {INT I = 0; File: // String Index Int ISOVER = 0; File: // Is String Over? String SQL = ""; File: / / Connect database dbbean dbconn = new dbbean (); file: // Initialization RSAR, RSFILE RSFILE = "SELECT * FROM FOA_WJ WHERE ((f_status <>" Handedate ') AND (f_status <> "Archive') (f_status is null)) AND (F_MARKDEL <> 1) or (f_markdel is null) ORDER BY F_JNSXH "; RSAR =" SELECT * FROM FOA_WJ where (f_status = 'Archive') and ((Fcsys_MID> 0) and (Not fcsys_mid Is NULL)) AND (F_MARKDEL <> 1) or (f_markdel is null) ORDER BY F_JNSXH "; SQL =" Select * from fox_webpurview where f_username = ' s "'"; file: // Read from the database Take user conditions, initialize RSSQL try {dbrs = dbconn.executeQuery (SQL); file: // This only has a record IF (DBRS! = Null && dbrs.next ()) rsSQL = dbrs.getstring ("f_searchpurview") Catch (Exception E) {E.PrintStackTrace ();
} File: // Separate rsaddar and rsaddfile if (rsSQL == ") {system.out.println (" User custom condition is empty or no record! ");} Else {while ((isover = = 0)) {IF (((i <= rsSql.Length () - 5) && (rsSql.charat (i) == 'a')) {file: // The current file i = i 1; While (i <= rsSql.Length () - 5) && (rsSql.charat (i)! = '~')) {while ((i <= rssql.length () - 5) && (rssql.charat (i) ! = ':')) { i;} IF ((i <= rsSql.Length () - 5) && (rsSql.charat (i) == ')) {i = i 1; while ((i <= rsSql.Length () - 1) && (Rssql.Charat (i)! = '~')) {RsAddar = rsaddar ql.charat (i); i;}}}} L.Charat (i) == '~') {i = i 4; if (i == rsSql.length ()) {isover = 1; continued;} else { i; continue;
}} File: //system.out.println (rsSql.charat (i));} else} ((i <= rssql.length () - 5) && (rssql.charat (i) == 'f') ) {File: // Current file i = i 1; while ((i <= rsSql.length () - 5) && (rsSql.charat (i)! = '~')) {While ((i < = rsSql.Length () - 5) && (rsSql.charat (i)! = ':')) { i;} IF ((i <= rssql.length () - 5) && (Rssql.Charat ( I) == ')) {i = i 1; while ((i <= rsSql.Length () - 1) && (rsSql.charat (i)! =' ~ ')) {r rdfile = rsaddfile Rssql.Charat (i); i;}}}} f (rssql.charat (i) == '~') {i = i 4; IF ( i == rsSql.Length ()) {isover = 1; continue;} else { i; contractdar =}}}}}}}}; " "! "; rsaddfile = rsaddfile ";
" "! "; Break;}}} public string getarchivesql () {file: // Get the file string, the operation of RSAR and RSADDAR String Tempar =" ""; string tempadd = ""; int indexar = 0; File : // RSAR position int indexAdd = 0; file: // rsaddar position int infirst = 0; file: // Find the place where the RSAR is found ((RSAR.CHARAT (INDEXAR)! = '(') && (Indexar <= RSAR.LENGTH () - 1)) {Tempar = Tempar RSAR.Charat (Indexar); Indexar ;} file: // indexar now refers to the first (local tempar = tempar "" WHILE ((indexadd <= rsaddar.length () - 1) && (rsaddar.charat (indexadd)! = '!')) {While ((indexAdd <= rsaddar.length () - 1) && (Rsaddar.Charat (IndexAdd)! = ';')) {Tempadd = Tempadd Rsaddar.Charat (IndexAdd); IndexAdd; file: //system.out.println (Tempadd);} file: // A condition IF of the user ((indexAdd <= RsAddar.length () - 1) && (Rsaddar.Charat (indexAdd) == ';')) {indexAdd = indexadd 1; if (isfirst == 0) {TEMPAR = Tempar Tempadd " " and " " "; isfirst = 1;} else {tempar = tempar tempadd " " " and " " ";} Tempadd ="
"}} // The user's condition ends File: // Plus the conditions in RSAR; while ((indexar <= rsar.length () - 1) && (rsar.charat (indexar)! = '; ')) {Tempar = Tempar RSAR.Charat (Indexar); indexar;} tempar = tempar '; '; file: //system.out.println (TEMPAR); return tempar;} public string getUserFileSQL () {File: // Get file string, operate String Tempfile = ""; string tempfile = ""; int indexfile = 0; file: // r = 0; File: // RSADDAR position intirst = 0; file: // Locate the WHILE ((rsfile.charat (indexfile)! = '(") && (IndexFile <= rsfile.length () - 1)) {TempFile = TempFile Rsfile.Charat (IndexFile); IndexFile ;} file: // indexar now refers to the first (local tempfile = tempfile ""; While (indexadd <= rsaddfile.length () - 1) && (rsaddfile.charat (indexadd)! = '!')) {while ((indexadd <= rsaddfile.length () - 1) && (rsaddfile.charat) IndexAdd)! = ';')) {Tempadd = Tempadd RsaddFile.Charat (IndexAdd); indexadd; file: //system.out.println (Tempadd);} File: // Separate one of the user's condition IF ((indexAdd <= RSADFILE.LENGTH () - 1) && (rsaddfile.charat (indexadd) == ';')) {indexAdd = indexadd 1;
IF (isfirst == 0) {TEMPFILE = TempFile Tempadd " " and " " "; isfirst = 1;} else {tempfile = TempFile Tempadd " "and" "";} Tempadd = "";}} // user's condition ends File: // Plus the conditions in RSAR; while ((indexfile <= rsfile.length () - 1) && (rsfile.charat (indexfile)! = ' ;) {TempFile = TempFile }; indexfile;} tempfile = tempfile '; File: //system.out.println (TempFile); returnization;} / * public static Void main (string [] args) {string s = "; string ss =" "; getSQL AA = new getSQL (s); s); s = aa.getarchivesql (); ss = aa.getuserfilesql (); system.out. Println (s); system.out.println (SS);} * /} is good, all code, JDK under test passes the relevant resources: with sound new year fireworks and animation effects - html-code