Video Address: http: wwwtudoucomprogramsview4GIENz1qdp0 create BaseDaopackagecnwingflydao; importjavasqlConnection; importjavasqlDriverManager; importjavasqlResultSet; importj 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 ();}}