MySql performs the JDBC join (Add/delete/modify/query) Operation

Source: Internet
Author: User

MySql performs the JDBC join (Add/delete/modify/query) Operation

Video address: http://www.tudou.com/programs/view/4GIENz1qdp0/

Create BaseDao

Package cn. wingfly. dao; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class BaseDao {Connection con = null; Statement st = null; ResultSet rs = null;/*** get join ** @ return */public Connection getConnection () {try {// load the driver. This sentence can also be written as: Class. forName ("com. mysql. jdbc. driver "); Class. forName ("com. mysql. jdbc. drive R "). newInstance (); // connect to MySQL con = DriverManager. getConnection (" jdbc: mysql: // localhost: 3306/money_note? CharacterEncoding = UTF-8 "," root "," root ");} catch (Exception e) {e. printStackTrace ();} return con;}/*** close the data source */public void CloseConnection (Connection con, Statement s, ResultSet rs) {try {if (rs! = Null) {rs. close () ;}if (s! = Null) {s. close () ;}if (con! = Null) {con. close () ;}} catch (SQLException e) {e. printStackTrace ();}}}
Test class
Package cn. wingfly. dao; import java. SQL. connection; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class Test extends BaseDao {Connection con = null; Statement st = null; ResultSet rs = null;/*** query data */public void find () {con = getConnection (); // obtain the join try {st = con. createStatement (); rs = st.exe cuteQuery ("select * from app_user"); while (rs. next () {System. out. println ("No.:" + rs. getInt ("uuid") + ", name:" + rs. getString ("userName") + ", Gender:" + rs. getString ("sex") + ", birthday:" + rs. getString ("birthday") + ", address:" + rs. getString ("address") ;}} catch (SQLException e) {e. printStackTrace ();} finally {CloseConnection (con, st, rs); // close the connection}/*** add data */public void add () {con = getConnection (); try {st = con. createStatement (); int result = st.exe cuteUpdate ("insert into app_user (userName, passWord, sex, birthday, address) values ('zhao liying', 'wanying', 'female ', '1970-02-03 ', 'beijing') "); if (result> 0) {System. out. println ("inserted successfully");} else {System. out. println ("insertion failed") ;}} catch (SQLException e) {System. out. println ("insertion failed"); e. printStackTrace () ;}finally {CloseConnection (con, st, rs) ;}/ *** update data */public void update () {con = getConnection (); try {st = con. createStatement (); int result = st.exe cuteUpdate ("update app_user set address = 'henan 'where uuid = '3'"); if (result> 0) {System. out. println ("updated successfully");} else {System. out. println ("update failed") ;}} catch (SQLException e) {e. printStackTrace () ;}finally {CloseConnection (con, st, rs) ;}}/*** delete data */public void delete () {con = getConnection (); try {st = con. createStatement (); int result = st.exe cuteUpdate ("delete from app_user where uuid = '3'"); if (result> 0) {System. out. println ("deleted successfully");} else {System. out. println ("failed to delete") ;}} catch (SQLException e) {e. printStackTrace () ;}finally {CloseConnection (con, st, rs) ;}} public static void main (String [] args) {Test test = new Test (); // test. add (); // test. update (); test. delete (); test. find ();}}

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.