Connection to JDBC

Source: Internet
Author: User

Import Java.sql.connection;import java.sql.drivermanager;import java.sql.resultset;import java.sql.Statement; public class MyConnection {Connection con;public static void Main (string[] args) {myconnection mycon=new myconnection (); Connection c=mycon.getconnection ();}    Public Connection getconnection () {//driver name String Driver = "Com.mysql.jdbc.Driver";    The URL points to the database name to access score String url= "Jdbc:mysql://localhost:3306/score";    String url= "Jdbc:mysql://127.0.0.1:3306/score";     MySQL configuration username String user = "root";      MySQL configuration when the password String password = "123456"; try {//Load driver Class.forName (driver);//Continuous database con=drivermanager.getconnection (URL, user, password); if (!con.isclosed ()) SYSTEM.OUT.PRINTLN ("Database connection succeeded! The//statement is used to execute the SQL statement statement statement=con.createstatement ();//SQL statement to execute string sql= "select * from Student"; Result set resultset re=statement.executequery (SQL);//Traversal data while (Re.next ()) {String sid=re.getstring (1); String sname=re.getstring (2); String sage=re.getstring (3); StriNg ssex=re.getstring (4); String szhuanye=re.getstring (5); System.out.println ("Sid:" +sid+ "Sname:" +sname+ "Sage:" +sage+ "Ssex:" +ssex+ "Szhuanye:" +szhuanye);}}    catch (Exception e) {e.printstacktrace ();} return con;}}

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.