Connect MySQL with the JavaBean

Source: Internet
Author: User

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

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.