Java connection MySQL Database-code

Source: Internet
Author: User

Tools: Eclipse

MySQL5.7.17

MySQL Connection driver: Mysql-connector-java-5.1.43.jar

Load driver: I managed it with Maven.

Database connection Information:

Database name: Wuwei

Packet Name: Greeting

Port number: 3306

User name: Root

Password: ******

Store these in the Database.properties file.

Source:

1  PackageHadoop.mysql;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 ImportJava.nio.file.Files;6 Importjava.nio.file.Paths;7 Importjava.sql.Connection;8 ImportJava.sql.DriverManager;9 ImportJava.sql.ResultSet;Ten Importjava.sql.SQLException; One Importjava.sql.Statement; A Importjava.util.Properties; -  - /** the  *  - * @ClassName: SQL - * @Description: This program tests the database and the JDBC driver is correctly configured - * @author *** + * @date 2017-9-4 pm 11:27:22 - * +  */ A  Public classSQL { at      -     /** -      *  - * @Title: getconnection - * @Description: Gets A connection from the properties specified in the file database,properties -  in     * @throwsIOException -     * @throwsSQLException to     * @returnConnection +      */ -      Public StaticConnection getconnection ()throwsIOException, SQLException the     { *         //create a properties and load Database.properties $Properties props =NewProperties ();Panax Notoginseng         Try(InputStream in = Files.newinputstream (Paths.get ("h://java//com.autwit.www//src//main//resources// Database.properties "))) -                 { the props.load (in); +                 } A         //driver name theString drivers = Props.getproperty ("Jdbc.drivers" ) ; +         if(Drivers! =NULL) System.setproperty ("Jdbc.drivers", drivers); -         //the URL points to the database name you want to access Wuwei $String url = props.getproperty ("Jdbc.url" ) ; $         //Database user name -String username = props.getproperty ("Jdbc.username" ) ; -         //Password theString Password = props.getproperty ("Jdbc.password" ) ; -         Wuyi         returndrivermanager.getconnection (URL, username, password); the     } -     /** Wu      *  - * @Title: Runtest About * @Description: Create a connect with Mysql,then executing C (Create) R (read) U (Update) D (delete) $     * -     * @throwsSQLException -     * @throwsIOException -     * @returnvoid A      */ +      Public Static voidRuntest ()throwsSQLException, IOException the     { -         //declaring connection Objects $         Try(Connection con =getconnection ()) the         { the             //Create a statement class object to execute the SQL statement theStatement stat =con.createstatement (); theStat.executeupdate ("CREATE Table greeting (Message Char (20))") ; -Stat.executeupdate ("Insert into greeting values (' Hello world! ')") ; in             //ResultSet class, used to store the obtained result set!!  the             Try(ResultSet rs = stat.executequery ("Select * from Greeting")) the             { About                 /* the Notice: Even if you are very sure that you can search for records, you can not directly value RS before Rs.next ().  the This involves the storage method of the RS object. The inside is a pointer. Without next, the pointer doesn't even point to the record. the                 */ +String message = "";  -                 if(Rs.next ()) {//or while (Rs.next ()) theMessage = rs.getstring ("message"); Bayi                    if(Message = =NULL){  theMessage = "";  the                    }  - System.out.println (message); -                 }     the             } theStat.executeupdate ("drop table greeting") ;  -         }  the     }94      the     the      Public Static voidMain (string[] args)throwsSQLException, IOException { the         98 runtest (); About     }101}

Execution Result:

Reference: 1,http://www.cnblogs.com/centor/p/6142775.html

2,java Core Volume II

Java connection MySQL Database-code

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.