1.在官网上下载并安装MySQL
2.在IDEA中输入测试代码ConnectionDemo.java
import static java.lang.System.out;
import java.sql.*;
public class ConnectionDemo {
public static void main(String[] args)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/demo";
String user = "root";
String passwd = "";
try(Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
out.printf("已%s数据库连接%n",
conn.isClosed() ? "关闭" : "打开");
}
}
}
1.使用老师推荐的XAMPP,在官网进行下载
2.成功连接MySQL,如下图
3.用浏览器输入http://127.0.0.1
打开XAMPP。
4.SQL建立数据库:
CREATE SCHEMA demo;
5.建立数据库表:
use demo;
CREATE TABLE t_message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name CHAR(20) NOT NULL,
email CHAR(40),
msg TEXT NOT NULL
) CHARSET=UTF8;
1.在官网https://dev.mysql.com/downloads/connector/j/ 上下载驱动
mysql-connector-java-5.1.41-bin.jar
2.在IDEA中添加驱动,如下图:
3.运行示例代码,显示成功连接如下图
try{ Statement sql=con.createStatement();
}
catch(SQLException e ){}
2.处理查询结果
SQL查询语句对数据库的查询操作将返回一个ResultSet
对象,ResultSet
对象是按“列”(字段)组织的数据行构成
ResultSet rs = sql.executeQuery("SELECT * FROM students");
下图为若干方法:
3.关闭连接:
con.close();
ResultSet对象和数据库连接对象(Connection对象)实现了紧密的绑定,一旦连接对象被关闭,ResultSet对象中的数据立刻消失
4.顺序查找
使用next()
方法移到下一个数据行
5.控制游标
为了得到一个可滚动的结果集,需使用下述方法获得一个Statement对象
Statement stmt = con.createStatement(int type ,int concurrency);
常用的滚动查询方法:
Public boolean absolute(int row):
将游标移到参数row的指定行后Public int getRow();
得到当前游标所指向的行号6.条件与排序查找
一般格式: select 字段 from 表名 where 条件
(1) 字段值和固定值比较,例如:select name,height from mess where name=‘李四‘
(2) 字段值在某个区间范围,例如:select * from mess where height>1.60 and height<
=1.8
用order by
子语句对记录排序
1.更新
update 表 set 字段 = 新值 where <条件子句>
2.添加
insert into 表(字段列表) values (对应的具体的记录)
或
insert into 表 values (对应的具体的记录)
3.删除
delete from 表名 where <条件子句>
select * from mess where name like '%林%' order by name
教材学习有问题先去https://shimo.im/doc/1i1gldfsojIFH8Ip/看看,如果别人没有提出相同问题,可以编辑文档添加,然后把自己提出的问题复制到下面:
计算机管理
->服务
中停止MySQL的进程import static java.lang.System.out;
import java.sql.*;
public class ConnectionDemo {
public static void main(String[] args)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/demo?serverTimezone=UTC";
String user = "root";
String passwd = "";
try(Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
out.printf("已%s数据库连接%n",
conn.isClosed() ? "关闭" : "打开");
}
}
}
修改了两部分
Class.forName("com.mysql. cj .jdbc.Driver");
为根据错误提示添加驱动。String jdbcUrl = "jdbc:mysql://localhost:3306/demo ?serverTimezone=UTC ";
是根据错误提示添加time zone
信息。修改后成功连接
教材中代码调试有问题先去https://shimo.im/doc/1i1gldfsojIFH8Ip/看看,如果别人没有提出相同问题,可以编辑文档添加,然后把自己提出的问题复制到下面:
import static java.lang.System.out;
import java.util.Scanner;
public class MessageDAODemo {
public static void main(String[] args) throws Exception {
MessageDAO dao = new MessageDAO(
"jdbc:mysql://localhost:3306/demo?" +
"serverTimezone=UTC",
"root", "");
Scanner console = new Scanner(System.in, "Big5");
while(true) {
out.print("(1) 显示留言 (2) 新增留言:");
switch(Integer.parseInt(console.nextLine())) {
case 1:
dao.get().forEach(message -> {
out.printf("%d\t%s\t%s\t%s%n",
message.getId(),
message.getName(),
message.getEmail(),
message.getMsg());
});
break;
case 2:
out.print("姓名:");
String name = console.nextLine();
out.print("邮件:");
String email = console.nextLine();
out.print("留言:");
String msg = console.nextLine();
dao.add(new Message(name, email, msg));
}
}
}
}
What is the output of the following code?(下面代码的运行结果是?)
LocalDate date = LocalDate.of(2018, Month.APRIL, 40);
System.out.println(date.getYear() + " " + date.getMonth()
+ " "+ date.getDayOfMonth());
A .2018 APRIL 4
B .2018 APRIL 30
C .2018 MAY 10
D .Another date
E .The code does not compile.
F .A runtime exception is thrown.
**正解为F,因为一个月没有第四十天,所以报错
** ## 结对及互评
教材学习中的问题和解决过程, 一个问题加1分
代码调试中的问题和解决过程, 一个问题加1分
基于评分标准,我给本博客打分:XX分。得分情况如下:xxx
xxx
xxx
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 6/6 | 1/1 | 20/20 | |
第二周 | 245/251 | 1/2 | 18/38 | |
第三周 | 633/884 | 1/3 | 22/60 | |
第四周 | 305/1189 | 1/4 | 30/90 | |
第五周 | 410/1599 | 3/7 | 30/120 | |
第六周 | 1135/2734 | 3/10 | 30/150 | |
第七周 | 781/3515 | 3/13 | 30/180 | |
第八周 | 710/4225 | 3/16 | 30/210 | |
第九周 | 775/5000 | 2/18 | 30/240 |
尝试一下记录「计划学习时间」和「实际学习时间」,到期末看看能不能改进自己的计划能力。这个工作学习中很重要,也很有用。
耗时估计的公式
:Y=X+X/N ,Y=X-X/N,训练次数多了,X、Y就接近了。
计划学习时间:XX小时
实际学习时间:XX小时
改进情况:
(有空多看看现代软件工程 课件
软件工程师能力自我评价表)
原文:https://www.cnblogs.com/thz666/p/10778690.html