The most standard Java MySQL connection and javamysql connection

Source: Internet
Author: User

The most standard Java MySQL connection and javamysql connection

Package com. runoob. test; import java. SQL. *; public class MySQLDemo {// JDBC driver name and Database URL static final String JDBC_DRIVER = "com. mysql. jdbc. driver "; static final String DB_URL =" jdbc: mysql: // localhost: 3306/RUNOOB "; // username and password of the database, you need to set static final String USER = "root"; static final String PASS = "123456"; public static void main (String [] args) {Connection conn = null; statement stmt = null; tr Y {// register the JDBC Driver Class. forName ("com. mysql. jdbc. driver "); // open the link System. out. println ("connect to the database... "); conn = DriverManager. getConnection (DB_URL, USER, PASS); // execute the query System. out. println ("instantiate the Statement pair... "); stmt = conn. createStatement (); String SQL; SQL = "SELECT id, name, url FROM websites"; ResultSet rs = stmt.exe cuteQuery (SQL); // expand the result set database while (rs. next () {// retrieves int id = rs through a field. getInt ("id"); String n Ame = rs. getString ("name"); String url = rs. getString ("url"); // output data System. out. print ("ID:" + id); System. out. print (", site name:" + name); System. out. print (", site URL:" + url); System. out. print ("\ n");} // close rs after completion. close (); stmt. close (); conn. close ();} catch (SQLException se) {// process the JDBC error se. printStackTrace ();} catch (Exception e) {// process Class. forName error e. printStackTrace ();} finally {// close the resource try {If (stmt! = Null) stmt. close ();} catch (SQLException se2) {}// do nothing try {if (conn! = Null) conn. close () ;}catch (SQLException se) {se. printStackTrace () ;}} System. out. println ("Goodbye! ");}}

 

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.