Import java.sql.*;p Ublic class Mysqlbean {private Connection con = null; Private ResultSet RS; Private String drivername = "Com.mysql.jdbc.Driver"; Private String serverName = "localhost"; Private String MyDatabase = "user"; Private String username = "root"; Private String Password = "138717"; Public Mysqlbean () {try {String url = "jdbc:mysql://" + serverName + "/" + MyDatabase;//A Class.forName (drivername); con = drivermanager.getconnection (URL, username, password); con = drivermanager.getconnection ("jdbc:mysql://" + ServerName + ": 3306/" + MyDatabase + "? user=" + Use Rname + "&password=" + password + "&useunicode=true&characterencoding=ut F-8 "); Con.setautocommit (FALSE); } catch (ClassNotFoundException e) {//Could not find the database driver System.out.println ("Could Not find the database DRiver "); } catch (SQLException e) {//Could not connect to the database System.out.println (' Could not Connec T to the database "); }} public Connection Getcon () {return con; } public ResultSet Getrs (String sql) throws Exception {try {Statement stmt = con.createstatement ( Resultset.type_scroll_insensitive, resultset.concur_read_only); rs = stmt.executequery (SQL); } catch (Exception e) {e.printstacktrace (); } return RS; } public int executeupdate (String sql) {int count = 0; try {Statement stmt = con. createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); Count = stmt.executeupdate (SQL); if (count! = 0); Con.commit (); else; Con.rollback (); } catch (SQLException ex) {ex.printstacktrace (); } return count; } public void Close () {try {if (con! = null) con.close (); } catch (Exception e) {e.printstacktrace (); } }}
Connect MySQL with the JavaBean