mysql学习 select查询 (stage1-4)

练习代码:

//查询tmp2表示的所有字段记录
select * from tmp2;

//查询city表中的满足 name字段的记录中包含有字母'o'或 字母'R'的记录,显示出name,countrycode字段记录值(不区分大小写)
use world; 
select name,countryCode from  city where name regexp 'O|R';

//查询city表示,name字段记录中包含有大写字母‘M’的记录,显示出name字段记录值(区分大小写 binary )
use world;
select name from city where binary name regexp 'M';

//查询city表示满足name 字段记录中包含有字母‘a’或者'b'或者'c’或者‘d’的记录值(不区分大小写)
select name from city where name regexp '[abcd]';

//查询city表中name包含有小数点的记录数,显示出name字段的记录值(因为小数点在mysql当中是一个特殊字符,表示所有的记录,因为需要使用两斜杠来进行转义操作)
select name from city where name regexp '\\.';

//查询countrylanguage表中满足percentage记录中包含数字的,且数字的个数不少于3个的记录,显示countrycode,language,percentage字段的记录值
select countrycode,language,percentage  from countrylanguage where percentage regexp '[[:digit:]]{3,}'; 

//查询countrylanguage表中满足percentage字段记录中以数字0-9开头或者以小数点开头的记录值,显示countrycode和percentage的记录值
select countrycode,percentage from countrylanguage where percentage regexp '^[0-9\\.]';

//查询country表中满足 region字段记录中以字母'e'结尾的记录,显示出name,region字段的记录值
select  name,region from country where region regexp 'e$'; 

//查询country表中满足name字段记录中不包含字母'e'的记录,显示name的记录值
select name from country where name regexp '[^e]';

//查询country表中的字段,将两个字段进行拼接,使用concat函数
select concat(name,'(',code,')')  from country;

//为新组合的字段指定别名,可以使用as+别名名称或者 空格+别名名称 的方式
select concat(name,'(',code,')') as countrycode from country;

//左截取字符串函数 left(字符串,截取长度)
select left('abcdefg',3);
结果:abc

//求字符串的长度 length(字符串)
select length('abcdefg');
结果:7

//查询city表中,字母'a'存在于字段name中的记录的值,显示出符合条件的name字段的值,使用 locate(子串,母串),返回的是子串在母串中的位置
use world;
select name from city  where locate('a',name)>0;
JavaFX是一个用于创建丰富互动用户界面的Java库,而MySQL是一种常用的关系型数据库。网络编程是指在不同计算机之间通过网络进行数据交互的编程技术。 要在JavaFX中使用MySQL进行网络编程,您可以遵循以下步骤: 1. 导入MySQL驱动程序:首先,您需要将MySQL驱动程序添加到JavaFX项目中。您可以下载MySQL Connector/J驱动程序并将其添加到项目的类路径中。 2. 建立数据库连接:使用MySQL Connector/J提供的API,在Java代码中建立与MySQL数据库的连接。您需要提供数据库的URL、用户名和密码。 3. 执行SQL查询:使用连接对象创建一个Statement对象,并使用该对象执行SQL查询。您可以编写SELECT、INSERT、UPDATE等SQL语句来操作数据库。 4. 处理查询结果:根据您的需求,使用ResultSet对象来处理SQL查询的结果。您可以从结果集中检索数据,并在JavaFX界面上显示或处理数据。 5. 关闭连接:当您完成所有数据库操作后,记得关闭数据库连接,以释放资源。 下面是一个简单的示例代码,演示了使用JavaFX和MySQL进行网络编程的基本步骤: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.sql.*; public class Main extends Application { private static final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; private static final String DB_USER = "username"; private static final String DB_PASSWORD = "password"; @Override public void start(Stage primaryStage) { Label resultLabel = new Label(); VBox root = new VBox(resultLabel); Scene scene = new Scene(root, 300, 200); try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM mytable")) { StringBuilder sb = new StringBuilder(); while (rs.next()) { String name = rs.getString("name"); int age = rs.getInt("age"); sb.append("Name: ").append(name).append(", Age: ").append(age).append("\n"); } resultLabel.setText(sb.toString()); } catch (SQLException e) { e.printStackTrace(); } primaryStage.setTitle("JavaFX MySQL Example"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 请注意,这只是一个简单的示例,您需要根据自己的实际需求进行修改和扩展。此外,为了确保安全性,请避免直接在JavaFX应用程序中执行敏感的SQL查询,应该使用服务器端脚本或API来处理数据库操作,并通过网络与JavaFX应用程序进行通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值