This article mainly introduces how to connect, query and modify the MySQL database in Java. Friends in need can refer to
0. General process:
(1) Call the Class.forName() method to load the driver.
(2) Call the getConnection() method of the DriverManager object to obtain a Connection object.
(3) Create a Statement object and prepare a SQL statement. This SQL statement can be a Statement object (a statement to be executed immediately), a PreparedStatement statement (a precompiled statement) or a CallableStatement object (a stored procedure call). statement).
(4) Call methods such as excuteQuery() to execute SQL statements and save the results in the ResultSet object; or call methods such as executeUpdate() to execute SQL statements without returning the results of the ResultSet object.
(5) Perform display and other equivalent processing on the returned ResultSet object.
(6) Release resources.
1. Connect to the database
(1) Download the Mysql connection driver
After downloading, place it in F:\Doctoral Research Unzip it in Information\Database Learning\mysql related program files.
(2) Load the JDBC driver
Operation method: In Eclipse, select the corresponding project, click Java Build Path in Project-Properties, and add mysql-connector-java- in Libraries 5.1.21-bin.jar, click OK.
(3) Build a simple database as follows:
import java.sql.*; public class GetConnection { public static void main(String[] args){ try{ //调用Class.forName()方法加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载MySQL驱动!"); }catch(ClassNotFoundException e1){ System.out.println("找不到MySQL驱动!"); e1.printStackTrace(); } String url="jdbc:mysql://localhost:3306/mysql"; //JDBC的URL //调用DriverManager对象的getConnection()方法,获得一个Connection对象 Connection conn; try { conn = DriverManager.getConnection(url, "root",""); //创建一个Statement对象 Statement stmt = conn.createStatement(); //创建Statement对象 System.out.print("成功连接到数据库!"); stmt.close(); conn.close(); } catch (SQLException e){ e.printStackTrace(); } } }
2. Query the data table
When querying a data table, you need to use the ResultSet interface, which is similar to a data table. Through the instance of this interface, you can obtain the retrieval result set and the interface information of the corresponding data table.
import java.sql.*; public class SelectTable { public static void main(String[] args){ try{ //调用Class.forName()方法加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载MySQL驱动!"); String url="jdbc:mysql://localhost:3306/aniu"; //JDBC的URL Connection conn; conn = DriverManager.getConnection(url, "root",""); Statement stmt = conn.createStatement(); //创建Statement对象 System.out.println("成功连接到数据库!"); String sql = "select * from stu"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql);//创建数据对象 System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄"); while (rs.next()){ System.out.print(rs.getInt(1) + "\t"); System.out.print(rs.getString(2) + "\t"); System.out.print(rs.getInt(3) + "\t"); System.out.println(); } rs.close(); stmt.close(); conn.close(); }catch(Exception e) { e.printStackTrace(); } } }
3. Modify and delete the database
//修改删除数据 import java.sql.*; public class UpdateDeleteDemo { public static void main(String[] args)throws Exception{ try{ //调用Class.forName()方法加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); System.out.println("成功加载MySQL驱动!"); String url="jdbc:mysql://localhost:3306/aniu"; //JDBC的URL Connection conn; conn = DriverManager.getConnection(url, "root",""); Statement stmt = conn.createStatement(); //创建Statement对象 System.out.println("成功连接到数据库!"); //查询数据的代码 String sql = "select * from stu"; //要执行的SQL ResultSet rs = stmt.executeQuery(sql);//创建数据对象 System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄"); while (rs.next()){ System.out.print(rs.getInt(1) + "\t"); System.out.print(rs.getString(2) + "\t"); System.out.print(rs.getInt(3) + "\t"); System.out.println(); } //修改数据的代码 String sql2 = "update stu set name=? where number=?"; PreparedStatement pst = conn.prepareStatement(sql2); pst.setString(1,"8888"); pst.setInt(2,198); pst.executeUpdate(); //删除数据的代码 String sql3 = "delete from stu where number=?"; pst = conn.prepareStatement(sql3); pst.setInt(1,701); pst.executeUpdate(); ResultSet rs2 = stmt.executeQuery(sql);//创建数据对象 System.out.println("编号"+"\t"+"姓名"+"\t"+"年龄"); while (rs.next()){ System.out.print(rs2.getInt(1) + "\t"); System.out.print(rs2.getString(2) + "\t"); System.out.print(rs2.getInt(3) + "\t"); System.out.println(); } rs.close(); stmt.close(); conn.close(); }catch(Exception e) { e.printStackTrace(); } } }
The above is the detailed content of Detailed explanation of how to use Java to query, modify and connect MySQL. For more information, please follow other related articles on the PHP Chinese website!