Learn notes _jdbc_1_demo1_ basic operations and procedures for connecting to a database

Source: Internet
Author: User

Common error: When connecting to a database, this sentence string url = "jdbc:mysql://localhost/database name"; The database name may not be the same as the database name you built

1, install Mysql, remember at this time you set the name of the database, here I set is:mysql, Password: 123,root (one of the common mistakes, the back of the book when writing the database to the name of MYDB3, and I name is Mysql)

2, Myeclise built a Java project, build a class, note that at this time to guide the package, JDBC package, the Guide package operation directly pull the compressed file in, and then also to the right to add to the path (one of the common errors did not right-click Import);

3, the establishment of a good class, run the program; Right outline you dmeo function, right-run as, the console below can see whether the link is successful, and if an exception can occur in JUnit one of the common see exception

The above red line, is my own two mistakes, the next attention.

. Java code

 PackageCn.idcast.demo1;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importorg.junit.Test; Public classDemo1 {/*** ClassNotFoundException: * > No driver package * > * * SQLException: * > Check 3 parameters: URL, username     , password is correct * > check if MySQL server is turned on! */@Test Public voidFUN1 ()throwsClassNotFoundException, SQLException {/** JDBC Four configuration parameters: * > DriverClassName:com.mysql.jdbc.Driver * > Url:jdbc:mysql://localhost: 3306/MYDB3 * > Username:root * > Password:123*/        /** All Java.sql.Driver implementations are provided with a static block, and the code inside the block is registering itself in the * drivermanager! */        /** After jdbc4.0, in each drive jar, a file named Java.sql.Driver is provided in the Meta-inf/services directory.         * The content of the file is the implementation class name of the interface! */Class.forName ("Com.mysql.jdbc.Driver");//load Driver Class (registered driver)//com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();//drivermanager.registerdriver (driver); //use URL, username, password to get the connection objectConnection con =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mysql", "root", "123");    can also be separated to write System.out.println (con); }}

Learn notes _jdbc_1_demo1_ basic operations and procedures for connecting to a database

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.