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