In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.