1. Install MySQL First
2. Second easy to operate MySQL should download a graphical interface to the database operating software: for example mysql-front or EMS SQL Manager
3. Install the EMS will prompt to set the root user's password, this password will be used to connect to the database later. For example, set to "126456"
4. Second, you want to compile the function containing the database operation to include the Mysql.jar in the JCreator
Configuration-Options-JDK profile-JDK version-edit-add archive, select Mysql.jar.
The operation of the database cannot contain two aspects: Load driver + Get Connection object, then all operations are based on connection object. Give the detailed steps for the database connected to MySQL:
Import java.sql.*;
public class RR
{
public static void Main (string[] args)
{
Try
{
String url= "Jdbc:mysql://localhost/mydb";
String user= "root";
String pwd= "123456";
Load driver, this sentence can also be written as: Class.forName ("Com.mysql.jdbc.Driver");
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Establish a connection to MySQL
Connection conn = drivermanager.getconnection (Url,user, PWD);
Execute SQL statement
Statement stmt = Conn.createstatement ();//Create statement object to execute SQL language
ResultSet rs = stmt.executequery ("SELECT * from student");
Working with result sets
while (Rs.next ())
{
String name = rs.getstring ("name");
SYSTEM.OUT.PRINTLN (name);
}
Rs.close ();//Close Database
Conn.close ();
}
catch (Exception ex)
{
System.out.println ("Error:" + ex.tostring ());
}
}
}
Java Connection MySQL Database