1.maven添加jar?pom.xml?中添加
1).
http://mvnrepository.com/习霹,或者:
https://search.maven.org/???添加
mysql
mysql-connector-java?5.1.47
java代碼連接數(shù)據(jù)庫:
* 代碼實現(xiàn):
? //1. 導(dǎo)入驅(qū)動jar包
? ? ? ? //2.注冊驅(qū)動
? ? ? ? Class.forName("com.mysql.jdbc.Driver");
? ? ? ? //3.獲取數(shù)據(jù)庫連接對象
? ? ? ? Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "root");
? ? ? ? //4.定義sql語句
? ? ? ? String sql = "update account set balance = 500 where id = 1";
? ? ? ? //5.獲取執(zhí)行sql的對象 Statement
? ? ? ? Statement stmt = conn.createStatement();
? ? ? ? //6.執(zhí)行sql
? ? ? ? int count = stmt.executeUpdate(sql);
? ? ? ? //7.處理結(jié)果
? ? ? ? System.out.println(count);
? ? ? ? //8.釋放資源
? ? ? ? stmt.close();
? ? ? ? conn.close();