已知:
1、maven对普通Java项目的创建,参考 https://www.cnblogs.com/wanjinliu/p/17706089.html 。
2、java常规访问mysql数据库,需要用到jdbc驱动。
调用的jar包,最新为“mysql-connector-j”--这个名字可以不记得,看见能认识它就行。
包、类入门用法,参考 https://www.cnblogs.com/wanjinliu/p/17359196.html
操作步骤:
1、maven新建项目(略)。记得按需修改jdk版本。
2、查找需要的maven包依赖项
在包查找网站(官网自行查找使用,国内可以用 http://mvn.coderead.cn/ )中,输入mysql,在弹出的搜索项中选择“mysql-connector-j”。
选择最新版本,得到其maven依赖配置。
3、把依赖内容复制到项目的pom.xml的对应位置
1 <dependency> 2 <groupId>com.mysql</groupId> 3 <artifactId>mysql-connector-j</artifactId> 4 <version>8.1.0</version> 5 </dependency> 6 </dependencies>
老规矩,前五行是内容,最后一行定位。
包的事情,交给maven。
4、书写代码:
1 package hc; 2 import java.sql.*; 3 public class App 4 { 5 public static void main(String[] args) 6 { 7 String url = "jdbc:mysql://xxx.xxx.xxx.xxx:xxx/d1?useSSL=false"; 8 String user = "root"; 9 String password = "xxxxxxxxx"; 10 String query; 11 12 Connection con; 13 Statement st; 14 ResultSet rs; 15 // connect 16 try 17 { 18 con = DriverManager.getConnection(url, user, password); 19 st = con.createStatement(); 20 21 } catch (SQLException ex) 22 { 23 24 System.out.println("connect wrong!"); 25 return; 26 } 27 28 // add 29 query = "insert into t1 values('ww',19)"; 30 try 31 { 32 st.execute(query); 33 34 } catch (SQLException ex) 35 { 36 System.out.println("insert wrong!"); 37 } 38 // select 1 39 query = "select * from t1"; 40 try 41 { 42 rs = st.executeQuery(query); 43 show_Resultset(rs); 44 45 } catch (SQLException ex) 46 { 47 System.out.println("select1 wrong!"); 48 } 49 // delete 50 query = "delete from t1 where xm='ww'"; 51 try 52 { 53 st.execute(query); 54 55 } catch (SQLException ex) 56 { 57 System.out.println("delete wrong!"); 58 } 59 // select 1 60 query = "select * from t1"; 61 try 62 { 63 rs = st.executeQuery(query); 64 show_Resultset(rs); 65 66 } catch (SQLException ex) 67 { 68 System.out.println("select1 wrong!"); 69 } 70 } 71 72 public static void show_Resultset(ResultSet rs) 73 { 74 75 try 76 { 77 ResultSetMetaData rsmd = rs.getMetaData(); 78 while (rs.next()) 79 { 80 for (int i = 1; i <= rsmd.getColumnCount(); i++) 81 { 82 System.out.print(rs.getString(i)); 83 } 84 System.out.println(); 85 } 86 } catch (Exception e) 87 { 88 System.out.println("show resultset wrong!"); 89 } 90 } 91 }
其中,7-9行设置数据库服务器。
5、编译运行
C:\Hc\1\p2>mvn compile [INFO] Scanning for projects... [INFO] [INFO] -------------------------------< hc:p2 >-------------------------------- [INFO] Building p2 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.0.2:resources (default-resources) @ p2 --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:\Hc\1\p2\src\main\resources [INFO] [INFO] --- compiler:3.8.0:compile (default-compile) @ p2 --- [INFO] Nothing to compile - all classes are up to date [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.750 s [INFO] Finished at: 2023-10-01T19:07:36+08:00 [INFO] ------------------------------------------------------------------------ C:\Hc\1\p2>mvn exec:java -Dexec.mainClass="hc.App" [INFO] Scanning for projects... [INFO] [INFO] -------------------------------< hc:p2 >-------------------------------- [INFO] Building p2 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec:3.1.0:java (default-cli) @ p2 --- zs20 ls18 ww19 zs20 ls18
成功。