Java connection MySQL Essay

Source: Internet
Author: User

The connection should be aware of all jars, as well as the environment to build;

Package util;

Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import com.mysql.jdbc.Connection;
Import com.mysql.jdbc.Statement;

public class Jdbc {
public static void Main (string[] args) {

Driver name

String Driver = "Com.mysql.jdbc.Driver";

The URL points to the database name you want to access Scutcs

String url = "Jdbc:mysql://127.0.0.1:3306/golden";

User name when MySQL is configured

String user = "root";

Java password when connecting to MySQL configuration

String password = "1234";


try {

Load Driver

Class.forName (driver);

Continuous database

Connection conn = (Connection) drivermanager.getconnection (URL, user, password);

if (!conn.isclosed ())

System.out.println ("Successfully connected! ");

statement used to execute SQL statements

Statement Statement = (Statement) conn.createstatement ();

The SQL statement to execute

String sql = "SELECT * from Golden.users";

Result set

ResultSet rs = statement.executequery (SQL);
System.out.println ("----------------------------------");
System.out.println ("Execution results are as follows:");
System.out.println ("----------------------------------");
System.out.println ("UserSID" + "\ T" + "usersname" + "\ T" + "pwd" + "\ T" + "age");
System.out.println ("----------------------------------");
String usersname = null;
String Pwd=null;
Integer age=0;
while (Rs.next ()) {

Select data

Usersname = rs.getstring ("Usersname");
Pwd=rs.getstring ("pwd");
Age=rs.getint (4); Index, also available as age;

Output results

  system.out.println (rs.getstring ("UserSID") + "\ T" + usersname+ "\ T" + pwd+ "\ T" + age);
  } 
  rs.close ();
  conn.close ();
  } catch (ClassNotFoundException e) {
   system.out.println ("Sorry,can ' t find the Driver! ");
   e.printstacktrace (); 
   } catch (SQLException e) {
     e.printstacktrace ();
    } catch (Exception e) {
     e.printstacktrace ();
     }  
  }  

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.