Java Operation MySQL Notes

Source: Internet
Author: User

The first step is to download the JDBC driver, point me. Then select the appropriate version.

After the download is complete, unzip and place the Mysql-connector-java-5.1.6-bin.jar file under the Java installation directory.

Everyone here has a different installation path and needs to find their own path.

The second step, you need to create a new database under MySQL, this is very simple, such as CREATE database test;

Then use test to switch to the database you are currently using.

You also need to create a new table, as some of our operations are done on the table below.

The third step is to load the JDBC driver and connect to the database.

The following code is for reference only and can be executed on my machine.

1 ImportJava.sql.*;2 Importjava.util.jar.JarException;3 4  Public classDBManager {5     //User name6     PrivateString user = "";7     //Password8     PrivateString Password = "";9     //HostTen     PrivateString host = ""; One     //Database name A     PrivateString database = ""; -     /* -      *  the * Private String - * url= "jdbc:mysql://" +host+ "/" + "useunicode=true&characterencoding=gb2312"; -      */ -     PrivateString url = ""; +     PrivateConnection con =NULL; - Statement stmt; +     /** A      *  at * Connect based on host, database name, database user name, database user password.  -      *  -      * @paramHost - * String -      *  -      * @paramDatabase in * String -      *  to      * @paramUser + * String -      *  the      * @paramPassword * * String $      */Panax Notoginseng      PublicDBManager (string host, string database, string user, string password) { -          This. Host =host; the          This. Database =database; +          This. user =user; A          This. Password =password; the         //Show Chinese +          This. url = "jdbc:mysql://" + host + "/" +database; -         Try { $Class.forName ("Com.mysql.jdbc.Driver"); $}Catch(ClassNotFoundException e) { -SYSTEM.ERR.PRINTLN ("Class not found:" +e.getmessage ()); -         } the         Try { -con = drivermanager.getconnection ( This. URL, This. User,Wuyi                      This. password); the             //The connection type is resultset.type_scroll_insensitive, -             //resultset.concur_read_only Wustmt =con.createstatement (resultset.type_scroll_insensitive, - resultset.concur_read_only); About}Catch(SQLException a) { $SYSTEM.ERR.PRINTLN ("SQL exception:" +a.getmessage ()); -         } -     } -     /** A      *  + * Returns the connection made the      */ -      PublicConnection Getcon () { $         returncon; the     } the     /** the      *  the * Execute a simple query statement -      *  in * Returns the result set obtained the      */ the      PublicResultSet executeQuery (String sql) { AboutResultSet rs =NULL; the         Try { thers =stmt.executequery (SQL); the}Catch(SQLException e) { + e.printstacktrace (); -         } the         returnrs;Bayi     } the     /** the      *  - * Execute a simple UPDATE statement -      *  the * Returns True if execution succeeds the      */ the      Public Booleanexecuteupdate (String sql) { the         Booleanv =false; -         Try { thev = stmt.executeupdate (sql) > 0?true:false; the}Catch(SQLException e) { the e.printstacktrace ();94}finally { the             returnv; the         } the     }98      Public Static voidMain (string[] args)throwsjava.lang.nullpointerexception{ About ResultSet rs; -DBManager exe =NewDBManager ("127.0.0.1", "Test", "root", "118118");101 102rs = Exe.executequery ("SELECT * FROM Student");103         104         Try { theResultSetMetaData meta_data = Rs.getmetadata ();//Column Name106              for(intI_col = 1; I_col <= Meta_data.getcolumncount (); i_col++) {107System.out.print (Meta_data.getcolumnlabel (i_col) + "");108             }109 System.out.println (); the              while(Rs.next ()) {111                  for(intI_col = 1; I_col <= Meta_data.getcolumncount (); i_col++) { theSystem.out.print (rs.getstring (i_col) + "");113                 } the System.out.println (); the             } the}Catch(Exception e) {117 118         }119     } -}
View Code

Java Operation MySQL Notes

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.