Jdbc connection to mysql database _ MySQL

Source: Internet
Author: User
Jdbc connection to mysql database Download the jdbc driver to the official website and place it under the relevant jdk directory, or add the jar file to the project

Package test_mysql; import java. SQL. *; import java. util. set; public class testjdbc {public static Connection getConnection () throws ClassNotFoundException, SQLException {String URL = "jdbc: mysql: // localhost: 3306/employees "; // load the driver Class. forName ("com. mysql. jdbc. driver "); String user =" root "; String passwd =" "; // the default password is null // data of the selected database // DriverManager. // Connection con = DriverManager. getConnection (URL, user, passwd); // Connection con = DriverManager. getConnection (url, user, password); String test_url = ""; // String my_url = "jdbc: mysql: // localhost: 3306; DatabaseName = employees; User = root; password = "; // test_url + = URL + 3306 +"; "+" DatabaseName = employees; "+" User = root; "+" Password = "+ ""; connection con = DriverManager. getConnection (URL, user, passwd); return con;} public static void main (String [] args) {String SQL = "select * fromdepartment S "; Statement statement = null; Connection con = null; ResultSet set = null; try {con = getConnection (); // con. setif (con = null) System. out. println ("con null" contains invalid statement=con.createstatement(;;set=statement.exe cuteQuery (SQL); // int n = set. getFetchSize (); // System. out. println (n); while (set. next () {System. out. println (set. getString (1) + "" + set. getString (2) ;}} catch (ClassNotFoundException e) {// TODO Auto-generated cat Ch blocke. printStackTrace ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (con! = Null) try {set. close (); statement. close (); con. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}

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.