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:
- CREATE DATABASE Scutcs;
Next, create the table:
- CREATE TABLE STUDENT
- (
- SNO CHAR (7) Not NULL,
- SNAME VARCHAR (8) Not NULL,
- SEX CHAR (2) Not NULL,
- Bdate DATE not NULL,
- HEIGHT DEC (5,2) DEFAULT 000.00,
- PRIMARY KEY (SNO)
- );
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 ();
}
}
}