数据库驱动(驱动jar包, 下载后放到项目下的lib目录并添加到libraries):
对于JDBC中定义接口(一套操作所有关系型数据库的规则)的实现类JavaWeb
Basics
创建一个Empty Project; 在Project Structure中设置Project SDK和Project
language level; 在Project Structure中Add New Module, 然后选中Java即可;
新建lib目录, 将jar包放在该目录, 右键jar包, 选中Add as Library,
在level处选中Module Library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mydb1" String username = "zyliang" String password = "test123" Connection conn = DriverManager.getConnection(url, username, password);
String sql = "update account set money=20000 where id=1"; String sql2 = "update account set money=20000 where id=2";
Statement stmt = conn.createStatement();
try{ conn.setAutoCommit(false); int count = stmt.executeUpdate(sql); int count2 = stmt.executeUpdate(sql2); System.out.println(count); System.out.println(count2); conn.commit(); }catch(Exception throwables){ conn.rollback(); throwables.printStackTrace(); }
stmt.close(); conn.close();
setAutoCommit(boolean autoCommit)
commit()
rollback()
executeUpdate(sql)
executeQuery(sql) public class Account{ private int id; private String name; private double money; }
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://127.0.0.1:3306/mydb1" String username = "zyliang" String password = "test123" Connection conn = DriverManager.getConnection(url, username, password);
String sql = "select * from account"; Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql); List<Account> list = new ArrayList();
while(rs.next()){ Account account = new Account(); int id = rs.getInt("id"); String name = rs.getString("name"); double money = rs.getDouble("money"); account.setId(id); account.setName(name); account.setMoney(money); list.add(account); }
System.out.println(list);
rs.close(); stmt.close(); conn.close();
|
目录文件: application.properties, sql.jar;
可能要修改application.properties文件中的username和password
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| jar包启动: java -jar .\\sql.jar
String url = "jdbc:mysql://127.0.0.1:3306/mydb1"
String url = "jdbc:mysql://127.0.0.1:3306/mydb1?useSSL=false&useServerPrepStmts=true" String username = "zyliang" String password = "test123" Connection conn = DriverManager.getConnection(url, username, password);
String name = "zhangsan"; String pwd = "123"; String sql = "select * from tb_user where username = '"+name+"' and password = '"+pwd+"'";
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); if(rs.next()){ System.out.println("登陆成功"); }else{ System.out.println("登录失败"); }
rs.close(); stmt.close(); conn.close();
log-output=FILE general-log=1 general_log_file="D:\\mysql.log" slow-query-log=1 slow_query_log_file="D:\\mysql_slow.log" long_query_time=2
|
优点: 资源重用; 提升系统响应速度;
避免数据库连接遗漏(设置了最大空闲时间)
标准接口: DataSource
Connection getConnection()
常用的数据库连接池: DBCP; C3P0; Druid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Properties prop = new Properties(); prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
Connection connection = dataSource.getConnection(); System.out.println(connection);
|
- Maven: 专门用于管理和构建Java项目的工具
poe.xml: Maven的配置文件
提供了一套标准化的项目结构、构建流程(编译、测试、打包、发布)、依赖管理机制
不同IDE之间创建的项目结构不一样
main/java: 源代码Java文件目录; main/resources: 源代码配置文件目录
Run Maven → compile / package …
1 2 3 4 5 6 7 8
| <dependencies> <dependency> <groupId>mysql<groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.32</version> </dependency> </dependencies>
|
本地仓库: 本机上的一个目录
中央仓库: 由Maven团队维护的全球唯一的仓库
地址: https://repo1.maven.org/maven2/
远程仓库(私服): 一般由公司团队搭建的私有仓库
若搭建远程仓库, 则jar包的查找顺序变为: 本地仓库→远程仓库→中央仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url><http: <mirrorOf>central</mirrorOf> </mirror>
mvn compile
mvn clean
mvn package
mvn test
mvn install
|
IDEA配置Maven: File→setting→搜索Maven→选择Maven中的Maven home
path、User settings file→Apply
Maven坐标(资源的唯一标识)的主要组成:
groupId: 当前Maven项目隶属组织的名称
artifactId: 当前Maven项目的名称
version: 当前项目版本号
创建Maven项目: Project Structure中选中Modules→选择Maven项目,
即可创建
导入Maven项目: 点击Maven界面中的”+“(Add Maven Projects),
找到需要导入的项目的poe.xml文件, 双击即可导入
若没有Maven界面: View→Appearance→Tool Window Bars
好用的Maven插件: Maven Helper
配置自动生效: File→Settings→Build Tools→Any changes
导入本地仓库jar包的快捷方式: 在poe.xml文件中按Alt+Insert
- JavaEE的三层架构:
表现层、业务层、持久层(负责将数据保存到数据库的那一层代码)