For my own JDBC always can't remember the question, I decided to write on the blog, every day to see-
Complete and workable examples:
1th:
Connect to JDBC
Public StaticString Jdbc_url = "jdbc:mysql://xxxxxxx:3306/mlhx_log_1?useunicode=true&autoreconnect=true& Characterencoding=utf-8&allowmultiqueries=true "; Public Static FinalString usr_name = "Xgame"; Public Static FinalString Usr_pass = "qingyouyile2013123456"; PrivateConnection con =NULL; PrivateStatement stmt =NULL; Public voidinit () {Try{class.forname ("Com.mysql.jdbc.Driver"); Con=drivermanager.getconnection (Jdbc_url, Usr_name, Usr_pass); stmt=con.createstatement (); System.out.println ("Connect SUCC!"); } Catch(ClassNotFoundException |SQLException e) {System.out.println ("Connect Problem"); E.printstacktrace (); } }
2nd:
Execute SQL statement
Public ResultSet Execute (String sql) { try { return stmt.executequery (SQL) ; Catch (SQLException e) { System.out.println ("Query problem:" + sql); E.printstacktrace (); } return NULL ; }
3rd:
Turn off the connection
Public void Close () { try { stmt.close (); Con.close (); System.out.println ("Close succ!" ); Catch (SQLException e) { System.out.println ("Close exception!" ); E.printstacktrace (); } }
The foundation of MYSQL--JDBC