MySQL database operations

Source: Internet
Author: User

Package cn.com. vanos. DB; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. resultset; import Java. SQL. sqlexception; import Java. SQL. statement; import Java. util. arraylist; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import Java. util. set; import Java. util. map. entry; import javax. XML. registry. deleteexception; public class dbmanager {Publ IC static string dbname = "vanos"; static connection conn = NULL; Public synchronized static connection openconnection () {try {class. forname ("com. mySQL. JDBC. driver "); string url =" JDBC: mysql: // 127.0.0.1: 3306/"+ dbname + "? Useunicode = true & characterencoding = UTF-8 "; Conn = drivermanager. getconnection (URL, "root", "root");} catch (exception e) {system. out. println ("exception:" + E. getmessage ();} return conn;} public static void main (string [] ARGs) throws sqlexception {openconnection (); // resultset rs = execsql ("describe t_user "); // Rs. next (); // system. out. println (RS. getstring ("field"); // select ("t_user", 2nd); // Select page, the maximum size of each page Value: 3 // selectpage ("t_user", 2, 3); // Getline ("t_user", "id = '1 '"); // Getline ("t_user", "username = 'hangzhou'"); // hashmap map = Getline ("t_user", "id = '8 '"); // map. put ("password", "12"); // Update ("t_user", map, "id = '8'"); Delete ("t_user ", "username = 'renwang'"); closeconn ();} Private Static Boolean execute (string SQL) {try {statement stat = Conn. createstatement (); system. out. print ("SQL:" + SQL); boo Lean ret = stat.exe cute (SQL); system. out. println ("[" + stat. getupdatecount () + "row (s) affected]");} catch (sqlexception e) {e. printstacktrace ();} return false;} public static void Delete (string table, string whereclause) {execute ("delete from" + Table + "where" + whereclause );} // update 'vanos '. 't_ user' // set 'email '= 'wac2056 @ 163.com', 'phonenum' = '000000' // Where 'id' = '8'; public static voi D Update (string table, hashmap tablevalues, string whereclause) {string sqlvalues = ""; set <entry <string, Object> entryset = tablevalues. entryset (); For (iterator <entry <string, Object> itr = entryset. iterator (); itr. hasnext ();) {entry = itr. next (); sqlvalues + = entry. getkey () + "= '" + entry. getvalue () + "',";} sqlvalues = sqlvalues. substring (0, sqlvalues. length ()-1); execute ("Update" + Table + "Set" + sqlvalues + "where" + whereclause);} // insert into 't_ user' ('id', 'username', 'Password', 'email ', 'phonenum') // values (1, 'renwang', '000000', 'wac056 @ 163.com ', '000000') public static void add (string table, hashmap tablevalues) {string sqlvalues = ""; string sqlfields = ""; set <entry <string, Object> entryset = tablevalues. entryset (); For (iterator <entry <string, Object> itr = entryset. iterato R (); itr. hasnext ();) {entry = itr. next (); sqlfields + = "'" + entry. getkey () + "',"; sqlvalues + = "'" + entry. getvalue () + "',";} sqlfields = sqlfields. substring (0, sqlfields. length ()-1); sqlvalues = sqlvalues. substring (0, sqlvalues. length ()-1); execute ("insert into" + Table + "(" + sqlfields + ") values (" + sqlvalues + ")");} /***** @ Param table name * @ Param curpage current page number * @ Param Maxitemsizethe size that a page contain * @ Param fieldswhat fields of table you want to get. */public static list selectpage (string table, int curpage, int maxitemsize, string... fields) throws sqlexception {return select (table, (curPage-1) * maxitemsize, (curpage) * maxitemsize, fields);} public static list select (string table, int min, int Max, string... fields) throws sqlexception {stri Ng filed2sql = ""; if (fields. length = 0) {filed2sql = "*";} else {for (string field: fields) {filed2sql + = "" + field + ",";} If (filed2sql! = NULL) filed2sql = filed2sql. substring (0, filed2sql. length ()-1);} resultset rs = execsql ("select" + filed2sql + "from" + Table + "Limit" + min + "," + max ); list 

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.