This article mainly uses MySQL as an example to illustrate how Java connects to the database. The specific content is as follows
Of course, first of all, you need to install JDK (usually JDK1.5.X). Then install MySQL, these are relatively simple, so I won’t talk about the specific process. After configuring these two environments, download the JDBC driver mysql-connector-java-5.0.5.zip (this is the latest version). Then unzip it into either directory. I unzip to disk D, and then add the mysql-connector-java-5.0.5-bin.jar in its directory to the classpath, as follows: "My Computer" -> "Properties" -> "Advanced" -> "Environment Variables" , edit the classpath in the system variables, add D:/mysql-connector-java-5.0.5/mysql-connector-java-5.0.5-bin.jar to the end, and add " ; " before adding this string to distinguish it from the previous classpath. Then make sure.
The environment is configured, it is very simple. Now, configure MySQL first, set its user name to "root" and its password to "root". Create Database on the command line or with a SQL front-end software.
I used SQLyog's front-end software to create Database.
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, you can use the SQL statement insert into <table name> values (value1, value2, ...);
You can also use SQLyog to operate
OK, create it.
Next, we will write a .java file to demonstrate how to access the MySQL database.
import java.sql.*;public class JDBCTest {public static void main(String[] args){ // Driver name String driver = "com.mysql.jdbc.Driver"; // URL points to the database name to be accessed String url = "jdbc:mysql://127.0.0.1:3306/scutcs"; // Username during MySQL configuration String user = "root"; // Password during MySQL configuration String password = "root"; try { // Load the driver Class.forName(driver); // Continuous database Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); // statement is used to execute SQL statement Statement statement = conn.createStatement(); // SQL statement to be executed String sql = "select * from student"; // ResultSet ResultSet rs = statement.executeQuery(sql); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ System.out.println("Execution result is as follows:"); System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // Then use the GB2312 character set to decode the specified byte array name = new String(name.getBytes("ISO-8859-1"),"GB2312"); // Output result System.out.println(rs.getString("sno") + "/t" + name); } 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(); } } }Next, let's run to see the effect:
D:/testjdbc>javac JDBCTest.javaD:/testjdbc>java JDBCTestSucceeded connecting to the Database!-------------------------------------------
The execution results are as follows:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The above are the detailed steps for Java to connect to MYSQL database. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.