navicatÔõôÅþÁ¬javaweb
ÔÚ java web ÖÐÅþÁ¬ navicat µÄ°ì·¨£ºÌí¼Ó mysql Çý¶¯Æ÷½¨ÉèÅþÁ¬ÊôÐÔÖ´ÐÐÅÌÎÊ
ÔõÑùÔÚ Java Web ÖÐÅþÁ¬ Navicat
°ì·¨Ò»£ºÌí¼Ó MySQL Çý¶¯Æ÷
ÔÚ IDE ÖУ¬ÓÒ¼üµ¥»÷ÏîÄ¿Îļþ¼Ð²¢Ñ¡Ôñ¡°Build Path¡± -> ¡°Add External JARs¡±¡£
ä¯ÀÀ²¢Ñ¡Ôñ MySQL Çý¶¯Æ÷ jar Îļþ£¨Í¨³£Î»ÓÚ Navicat ×°ÖÃĿ¼£©¡£
°ì·¨¶þ£º½¨ÉèÅþÁ¬ÊôÐÔ
ÔÚ Java ´úÂëÖУ¬½¨ÉèÒ»¸ö Connection ¹¤¾ß£º
DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");
µÇ¼ºó¸´ÖÆ
½«ÒÔÏÂÊôÐÔÌ滻ΪÄú×Ô¼ºµÄ MySQL Êý¾Ý¿âÐÅÏ¢£º
Á¬Ã¦Ñ§Ï°¡°JavaÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
localhost£ºÊý¾Ý¿âЧÀÍÆ÷Ö÷»úÃû»ò IP µØµã
3306£ºMySQL ĬÈ϶˿Ú
database_name£ºÒªÅþÁ¬µÄÊý¾Ý¿âÃû³Æ
username£ºMySQL Óû§Ãû
password£ºMySQL ÃÜÂë
°ì·¨Èý£ºÖ´ÐÐÅÌÎÊ
½¨ÉèÒ»¸ö Statement ¹¤¾ßÀ´Ö´ÐÐÅÌÎÊ£º
Statement statement = connection.createStatement();
µÇ¼ºó¸´ÖÆ
±àд²¢Ö´ÐÐ SQL ÅÌÎÊ£º
String query = "SELECT * FROM table_name"; ResultSet resultSet = statement.executeQuery(query);
µÇ¼ºó¸´ÖÆ
ÆÊÎö ResultSet ÒÔ»ñÈ¡ÅÌÎÊЧ¹û£º
while (resultSet.next()) { // »ñÈ¡ÁÐÖµ²¢´¦Àí }
µÇ¼ºó¸´ÖÆ
ʾÀý´úÂë
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) { try { // Ìí¼Ó MySQL Çý¶¯Æ÷ Class.forName("com.mysql.cj.jdbc.Driver"); // ½¨ÉèÅþÁ¬ Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // Ö´ÐÐÅÌÎÊ Statement statement = connection.createStatement(); String query = "SELECT * FROM table_name"; ResultSet resultSet = statement.executeQuery(query); // ÆÊÎöÅÌÎÊЧ¹û while (resultSet.next()) { System.out.println("ID: " + resultSet.getInt("id")); } // ¹Ø±Õ×ÊÔ´ resultSet.close(); statement.close(); connection.close(); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇnavicatÔõôÅþÁ¬javawebµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡