Get the App
SLTechnology News&Howtos  ›  Database  › 

How to connect to MySQL5.7 using JDBC

Shulou Source: shulou.com Published: 2022-05-31 22:21:40 09月16日 Update

Today, I will talk to you about how to use JDBC to connect to MySQL5.7. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. First, prepare the mysql and eclipse environment. After the environment is set up, download the jdbc driver package from the eclipse official website.

two。 Take the mysql-connector-java-5.1.31-bin.jar from the downloaded file, put it into the project, and import the path

Method: right-click the project name-> Build Path- > Configure Build Path, and select Add External JAR... Find the location of mysql-connector-java-5.1.31-bin.jar and load the driver package into the project

3. Write an example to test it

Package testmysql; import java.sql.*; public class Test {public static void main (String [] args) {String driver = "com.mysql.jdbc.Driver"; String URL = "jdbc:mysql://localhost:3306/student"; Connection con = null; try {Class.forName (driver);} catch (java.lang.ClassNotFoundException e) {System.out.println ("Connect Successfull.") System.out.println ("Cant't load Driver");} try {con=DriverManager.getConnection (URL, "root", "root"); System.out.println ("Connect Successfull.");} catch (Exception e) {System.out.println ("Connect fail:" + e.getMessage ());}

After connecting to the database, you can query the database table according to the contents of the table. First of all, there must be contents in the table. After entering some information into the table, you can use SQL language to query.

Import java.sql.*; public class Main {public static void main (String [] args) {String driver = "com.mysql.jdbc.Driver"; String URL = "jdbc:mysql://localhost:3306/xiaolu"; Connection con = null; ResultSet rs = null; Statement st = null; String sql = "select * from student"; try {Class.forName (driver) } catch (java.lang.ClassNotFoundException e) {/ / System.out.println ("Connect Successfull."); System.out.println ("Cant't load Driver");} try {con=DriverManager.getConnection (URL, "root", "root"); st=con.createStatement (); rs=st.executeQuery (sql) If (rswatches null) {ResultSetMetaData rsmd = rs.getMetaData (); int countcols = rsmd.getColumnCount (); for (int ibirthday 1 / i1) System.out.print (";"); System.out.print (rsmd.getColumnName (I) + ");} System.out.println (") While (rs.next ()) {System.out.print (rs.getString ("sno") + "); System.out.print (rs.getString (" sname ") +"); System.out.print (rs.getString ("ssex") + ""); System.out.print (rs.getString ("sage") + "") System.out.println (rs.getString ("sdept") + ");}} / / System.out.println (" Connect Successfull. "); System.out.println (" ok "); rs.close (); st.close (); con.close () } catch (Exception e) {System.out.println ("Connect fail:" + e.getMessage ());} after reading the above, do you have any further understanding of how to connect to MySQL5.7 using JDBC? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Content engineering data database environment query driver location example information location document method more knowledge article industry language information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MySQL Docker NVidia