Seven, JOBC database programmingSeven, JOBC database programmingOne, Java database programming steps1, the database driver package into the Lib directory;2, load driver--The entire operational database program only needs to be loaded once during operationclass.forname ("Com.mysql.driver"); Load driver driverentire Operations database program, only need to load once3, open the connection1, open the link call connection interface; (Login database)Connection con=null;con=drivermanager.getconnection ("address; Port/library; encoding format, account number, password);need to use try to catch errorsencoding format;turn on the code; Useunicode=trueUse & stitching parameters; &characterencoding=utf-8example;Connection con=null;con=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test? useunicode=true&characterencoding=utf-8 "," root "," root ");4, Operation Pipeline (interview Focus)1, additions and deletions;1, declaring the SQL statementString sql= "Update t_atmuser set f_atmuserpassword=?,f_atmusermoney=where pk_atmid=?; ";2,? Placeholder, delegate, use PreparedStatement for? string concatenation is risky (SQL injection),so use location assignment (the subscript is the first question mark from the beginning , the first one)preparedstatement ps=con.preparestatement (SQL);ps.setstring (1, User.getpassword ());ps.setfloat (2, User.getaccount ());Ps.setint (3, User.getatmid ());3, writes the database, returns a value of type int, the number of rows in responseps.executeupdate ();4, string concatenation using (Risky SQL injection)Statement stat = con.createstatement ();int n = stat.executeupdate (sql);2, check1, declare the statement (ID with ID check, faster)String sql= "SelectPk_atmid,f_atmusername,f_atmuserpassword,f_atmusermoneyFrom T_atmuser where f_atmusername=? and f_atmuserpassword=? ";2,? Placeholder, delegate, assign a value to a position with PreparedStatement(subscript is the first question mark from the beginning.)preparedstatement ps=con.preparestatement (SQL);ps.setstring (1, userName);ps.setstring (2, userpassword);3, returns a ResultSet type dataResultSet rs=ps.executequery ();4, use while to read all the rows of data, Rs.next () a few rows of data will return a few truewhile (Rs.next ()) {//rs.next (); Determine if there is a next liners.get data type (that field)User.setaccount (Rs.getfloat ("F_atmusermoney"));User.setatmid (Rs.getint ("Pk_atmid"));User.setcardnum (rs.getstring ("F_atmusername"));User.setpassword (rs.getstring ("F_atmuserpassword"));}5, close the pipeif (CON!=UNLL) {con.close ();}Two, transaction1, an action, at the same time, involves the addition and deletion of multiple tables operation, and these operations into one;2, must be guaranteed to be the sameConnection3, set auto commit as False (open transaction);Con.setautocommit (FALSE);//write under Connection4, finally with Con.commit (); manual submission; no exception occurred before ;5, the exception is executed Con.rollback (); rollback; a try (catch exception) is required6, 4 major features of a transaction1, Atomicmultiple actions are a whole, cannot be cut, must simultaneously succeed or fail simultaneously;2, consistencymultiple action execution results must be executed in sequence3, Isolationtransaction execution is not disturbed by other actions, and the intermediate result of transaction execution is transparent to other actions;4, Durabilityfor transactions that you have committed, you must ensure that you perform operations on the physical tables of the database;three, mapping;the association between a class and a class, and the association between a table and a tableFour, note1,date dt=new Date ();//java DateDate de=new java.sql.date (Tb.gettime ()), SQL date
Seven, JOBC database programming