Org.gjt.mm.mysql.Driver Database Classes

Source: Internet
Author: User
  1. Package Pubcls;
  2. Import java.sql.*;
  3. /**
  4. * MYSQL simple Operation class
  5. * @author Itblt
  6.  */  
  7. Public class Mysql
  8. {
  9. Public Connection Livecon = NULL ; 
  10. Public int dbwaittime = 3600;
  11. Public String DbLang = "" ; 
  12. Public String errmessage = "" ; 
  13. Private String Linkurl = "" ; 
  14. Private String linkdb = "" ; 
  15. Private String Linkuser = "" ; 
  16. Private String linkpwd = "" ; 
  17. /**
  18. * Set Default parameters
  19.      */  
  20. Private void SetDefault () {
  21. This . Linkurl = "jdbc:mysql://localhost:3306/" ; 
  22. This . linkdb = "Test" ; 
  23. This . Linkuser = "Root" ; 
  24. This . linkpwd = "" ; 
  25. This . DbLang = "UTF8" ; 
  26. }
  27. /**
  28. * Initialized with default parameters
  29.      */  
  30. Public Mysql () {
  31. This . SetDefault ();
  32. }
  33. /**
  34. * Specify database and encoding for initialization
  35. * @parem String SDB
  36. * Database name
  37. * @parem String Dblang
  38. * Coding (usually UTF8/GBK)
  39.      *
  40.      */  
  41. Public Mysql (String sdb, String Dblang) {
  42. This . SetDefault ();
  43. This . linkdb = SDB;
  44. This . DbLang = DbLang;
  45. }
  46. /**
  47. * Initialized with full parameters
  48. * @parem String surl
  49. * Connection URL (jdbc:mysql://ip: Port/): jdbc:mysql://localhost:3306/
  50. * @parem String SDB
  51. * Database name
  52. * @parem String suser
  53. * User name
  54. * @parem String spwd
  55. * Password
  56. * @parem String Dblang
  57. * Database Encoding
  58.      */  
  59. Public Mysql (String surl, String sdb, String suser, String spwd, String Dblang) {
  60. This . Linkurl = sURL;
  61. This . linkdb = SDB;
  62. This . Linkuser = suser;
  63. This . linkpwd = spwd;
  64. This . DbLang = DbLang;
  65. }
  66. /**
  67. * Get links
  68.     */  
  69. Public Connection getconnection () throws SQLException,
  70. Java.lang.ClassNotFoundException
  71. {
  72. This . Livecon = This . getconnection (this. Linkurl, this.linkdb, this. Linkuser, this. Linkpwd, this. DbLang);
  73. return this. Livecon;
  74. }
  75. /**
  76. * Get links with specified parameters
  77. * @parem String surl
  78. * Connection URL (jdbc:mysql://ip: Port/): jdbc:mysql://localhost:3306/
  79. * @parem String SDB
  80. * Database name
  81. * @parem String suser
  82. * User name
  83. * @parem String spwd
  84. * Password
  85. * @parem String Dblang
  86. * Database Encoding
  87. * @return Connection
  88.      */  
  89. Public Connection getconnection (String surl, String sdb, String suser, String spwd, string Dblang) throws SQLException,
  90. Java.lang.ClassNotFoundException
  91. {
  92. This . Linkurl = sURL;
  93. This . linkdb = SDB;
  94. This . Linkuser = suser;
  95. This . linkpwd = spwd;
  96. This . DbLang = DbLang;
  97. Class.forName ("Org.gjt.mm.mysql.Driver");
  98. This . Livecon = Drivermanager.getconnection (Surl+sdb, Suser, spwd);
  99. This . Prequery ();
  100. return this. Livecon;
  101. }
  102. /**
  103. * MySQL Escape
  104. * @return String
  105.      */  
  106. Public static string Quotestr (String str)
  107. {
  108.         string[] searchs = { ,   " n " ,   "R" ,   ,   "T" ,   ,   " " }; 
  109. String[] Replaces = {"yes", "\ n", "\ r" , "\z" , "\ T" ,  "\\", " \ "" ,  "\'" };
  110. for (int i=0; i < searchs.length; i++)
  111. {
  112. str = str.replace (Searchs[i], replaces[i]);
  113. }
  114. return str;
  115. }
  116. /**
  117. * Test Link
  118. * @return Boolean
  119.      */  
  120. Private boolean Testcon () throws SQLException
  121. {
  122. if (this. livecon==null | | This . livecon.isclosed ())
  123. {
  124. Try  
  125. {
  126. This . getconnection ();
  127. return true;
  128. catch(classnotfoundexception e)
  129. {
  130. return false;
  131. }
  132. Catch (SQLException E2)
  133. {
  134. This . Errmessage = E2.getmessage ();
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Execute Write statement
  142. * @parem string query
  143. * SQL Write class operations
  144. * @return int
  145.      */  
  146. Public int execupdate (String query) throws SQLException
  147. {
  148. if (! This . Testcon ())
  149. {
  150. return  - 1 ; 
  151. }
  152. Statement sql_statement = this. livecon.createstatement ();
  153. int Result = sql_statement.executeupdate (query);
  154. Sql_statement.close ();
  155. return Result ;
  156. }
  157. /**
  158. * Execute INSERT statement and return the last inserted primary chain ID
  159. * @parem String
  160. * SQL SQL statement
  161. * @return int
  162.      */  
  163. Public Long execinsert (String query) throws SQLException
  164. {
  165. if (! This . Testcon ())
  166. {
  167. return  - 1 ; 
  168. }
  169. Statement sql_statement = this. livecon.createstatement ();
  170. Long Result = sql_statement.executeupdate (query);
  171. if (result>0)
  172. {
  173. sql_statement = this. Getstatement ("select last_insert_id ();" );
  174. ResultSet rs = Sql_statement.getresultset ();
  175. Rs.next ();
  176. result = Rs.getlong (0);
  177. }
  178. Sql_statement.close ();
  179. return Result ;
  180. }
  181. /**
  182. * Get a recordset
  183. * @parem String query
  184. * SQL statements for queries
  185. * @return ResultSet
  186.      */  
  187. Public ResultSet getresultset (String query) throws SQLException
  188. {
  189. if (! This . Testcon ())
  190. {
  191. return null;
  192. }
  193. Statement sql_statement = this. Getstatement (query);
  194. ResultSet result = Sql_statement.getresultset ();
  195. return Result ;
  196. }
  197. /**
  198. * Obtain a recordset that can be used for update operations
  199. * @parem String query
  200. * SQL statements for queries
  201. * @return ResultSet
  202.      */  
  203. Public ResultSet getresultsetupdate (String query) throws SQLException
  204. {
  205. if (! This . Testcon ())
  206. {
  207. return null;
  208. }
  209. Statement sql_statement = this. Livecon.createstatement (Resultset.type_scroll_insensitive, resultset.concur_updatable);
  210. Sql_statement.executequery (query);
  211. ResultSet result = Sql_statement.getresultset ();
  212. return Result ;
  213. }
  214. /**
  215. * Get a statement
  216. * @parem String query
  217. * Query SQL
  218. * @return Statement
  219.      */  
  220. Public Statement getstatement (String query) throws SQLException
  221. {
  222. if (! This . Testcon ())
  223. {
  224. return null;
  225. }
  226. Statement sql_statement = this. Livecon.createstatement (Resultset.type_forward_only, RESULTSET.CONCUR_READ_ONLY);
  227. Sql_statement.executequery (query);
  228. return sql_statement;
  229. }
  230. /**
  231. * Select Database
  232.      */  
  233. Public void selectdb (String dbname) throws SQLException
  234. {
  235. This . Testcon ();
  236. This . Execupdate ("use")+ dbname +"';" );
  237. }
  238. /**
  239. * pretreatment
  240.      */  
  241. Private void prequery () throws SQLException
  242. {
  243. This . Execupdate ("SET NAMES '"+ this. DbLang +"", character_set_client= Binary, sql_mode= ', wait_timeout='+ this. Dbwaittime +", interactive_timeout = "+ this. Dbwaittime +"; );
  244. }
  245. /**
  246. * Close Link
  247.     */  
  248. Public void close () throws SQLException
  249. {
  250. if (this. Livecon!= null &&! This . livecon.isclosed ())
  251. {
  252. This . Livecon.close ();
  253. }
  254. This . Livecon = NULL ; 
  255. }
  256. /**
  257. * Resource Analysis
  258.     */  
  259. Public void Finalize () {
  260. Try  { 
  261. This . Close ();
  262. catch(SQLException e) {
  263. E = null;
  264. }
  265. }
  266. }//end class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.