Java connection to database MySQL method

Source: Internet
Author: User

1. Configure the file with environment variables,

2. Create DATABASE, insert data

Note the place:

(1) environment variable CLASSPATH (can be uppercase, can also be lowercase, can be placed in personal variables, you can also try system variables)

The value inside F:\mysql-connector-java-5.1.10-bin.jar must be a bit and semicolon, as follows

.; F:\mysql-connector-java-5.1.10-bin.jar

(2) in Eclipse if the configuration is good, add mysql--connect can. Does not need to be inside the environment variable, nor does it need to be in the environment variable (because it is useless, pro-test)

Create the database First:

    1. CREATE DATABASE Scutcs;

Next, create the table:

    1. CREATE TABLE STUDENT
    2. (
    3. SNO CHAR (7) Not NULL,
    4. SNAME VARCHAR (8) Not NULL,
    5. SEX CHAR (2) Not NULL,
    6. Bdate DATE not NULL,
    7. HEIGHT DEC (5,2) DEFAULT 000.00,
    8. PRIMARY KEY (SNO)
    9. );

Then insert the data with the SQL statement insert into < table name > values (value1, value2, ...);

INSERT into student (Sno,sname,sex,bdate) VALUES (101, ' Li Xinghua ', ' Male ', ' 1998-02-03 ');

INSERT into student (sno,sname,sex,bdate) VALUES (102, ' Wang Hing Wah ', ' Male ', ' 1988-02-03 ');

Method One:

Package JDBC2;

Import java.sql.*;
public class JDBCTest3 {
public static void Main (string[] args) {
String dbdriver= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql://127.0.0.1:3306/scutcs";
String user= "root";
String password= "admin";
try{

String url = "Jdbc:mysql://localhost:3306/bizhi?"
+ "User=root&password=admin&useunicode=true&characterencoding=utf8";
Class.forName (Dbdriver);

SYSTEM.OUT.PRINTLN ("Load MySQL driver successfully");


}catch (ClassNotFoundException e) {
System.out.println ("Sorry,cannot Find the Driver");
E.printstacktrace ();
}
}
}

Method Two:

Import java.sql.*;
public class JDBCTest4 {
 public static void Main (string[] args) {
    String dbdriver= " Com.mysql.jdbc.Driver ";
    String url= "Jdbc:mysql://127.0.0.1:3306/scutcs";
    String user= "root";
    String password= "admin";   
    try{
     //Method II:
     class.forname (dbdriver);
       Connection conn=drivermanager.getconnection (url, user, password);
     if (!conn.isclosed ()) System.out.println ("Successfully connected to Database II");  
    &NBSP
       
   }catch (classnotfoundexception e) {
     system.out.println ("Sorry,cannot Find the Driver");
     e.printstacktrace ();
   }

Method Two is more than the following catch exception
catch (SQLException e) {
E.printstacktrace ();

}catch (Exception e) {
E.printstacktrace ();
}
}

}

---------------------------------------------------------------------------

An example of a successful database connection

Import java.sql.*;
public class JDBCTest2 {
public static void Main (string[] args) {
String driver= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql://127.0.0.1:3306/scutcs";
String user= "root";
String password= "admin";

try{

Class.forName (driver);
Connection conn=drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ()) System.out.println ("database successfully connected");
Statement statement=conn.createstatement ();
String sql= "SELECT * from student";
ResultSet rs=statement.executequery (SQL);
System.out.println ("------------------");
System.out.println ("School number" + "\ T" + "name");
System.out.println ("-----------------");
String Name=null;
while (Rs.next ()) {
Name=rs.getstring ("sname");
System.out.println (rs.getstring ("sno") + "\ T" +name);

}
Rs.close ();
Conn.close ();
}catch (ClassNotFoundException e) {
System.out.println ("Sorry,cannot Find the Driver");
E.printstacktrace ();
}catch (SQLException e) {
E.printstacktrace ();

}catch (Exception e) {
E.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.