Java (Eclipse) and database (MySQL) connections

Source: Internet
Author: User
Tags mysql code

The connection between Java and data, we usually use the database is mysql,oracle, but in small and medium-sized enterprises basically use MySQL, the use of Oracle company is basically a national or global enterprise, with less comparison.

 Public voidgetconnection () {Connection con=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {            //get the database driver name (MySQL database)Class.forName ("Com.mysql.jdbc.Driver"); //database connection, DVD is the database name, root is the data name, 123456 is the data passwordCon =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/dvd", "root", "123456"); //Execute MySQL statementString sql = "SELECT * FROM DVD1"; //instantiating preprocessing statement objectsstmt =con.createstatement (); //Execute MySQL coders =stmt.executequery (SQL); //processing the resulting data             while(Rs.next ()) {intid = rs.getint ("id"); String name= rs.getstring ("name"); String State= Rs.getstring ("state"); String Date= Rs.getstring ("Date1"); intCount = Rs.getint ("Count1"); System.out.println (ID+ "\ T" + name + "\ T" + state + "\ T" + date + "\ T" +count); }        } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally{//guaranteed to shut down between database and Eclipse//Close the database            Try{rs.close ();                Stmt.close ();            Con.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }

This is the basic operation in learning, you can put the above MySQL implementation, put eclipse and data in a method, the next time you use the direct call

 public   Connection getconnection () {  try   { //  load JDBC driver  class.forname ("Com.mysql.jdbc.Driver" );  //  connect database  con = Drivermanager.getconnection ( "jdbc:mysql://localhost:3306/zxd_0922", "root", "123456"); //  Create statement object } catch          (Exception e) {e.printstacktrace ();     return   con; }

Each time you call, you need to accept con data,

Con=m.getconnection ();

This is the MySQL and Eclipse connection, the first time to write a blog, I hope you have a lot of support, there is a mistake to point out that you have a good help.

Java (Eclipse) and database (MySQL) connections

Related Article

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.