File name: Hello.java
Import java.sql.*;
public class Hello {
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 mytest
String url = "Jdbc:mysql://127.0.0.1:3306/mytest";
User name when MySQL is configured
String user = "root";
Password for MySQL configuration
String password = "123456";
try {
Load Driver
Class.forName (driver);
Connecting to a database
Connection conn = drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ())
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
statement used to execute SQL statements
Statement Statement = Conn.createstatement ();
The SQL statement to execute
String sql = "SELECT * from 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 ("username" + "\ T" + "password");
System.out.println ("-----------------");
String name = NULL;
while (Rs.next ()) {
Select username this column of data
Name = rs.getstring ("username");
First, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array.
The specified byte array is then decoded using the GB2312 character set
name = new String (name.getbytes ("iso-8859-1"), "GB2312");
Output results
SYSTEM.OUT.PRINTLN (name + "\ T" + rs.getstring ("passwd"));
}
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 ();
}
}
}
Java connection MySQL