JDBC 入门(三)_MySQL
JDBC
创建表
首先,我们在我们的示例数据库创建其中一张表 COFFEES,包含在咖啡店所卖咖啡的必要的信息,包括咖啡名字,他们的价格,本星期卖了多少磅及迄今为止卖的数目。关于 COFFEES 表我们以后会详细描述,如下:
COF_NAME SUP_ID PRICE SALES TOTAL
Colombian 101 7.99 0 0
French_Roast 49 8.99 0 0
Espresso 150 9.99 0 0
Colombian_Decaf 101 8.99 0 0
French_Roast_Decaf 49 9.99 0 0
存储咖啡名的列是 COF_NAME,它的 SQL 数据类型是 VARCHAR,最大的长度为 32 个字符。因为我们所卖的每种类型咖啡都使用不同的名字,名字可用于作为唯一识别咖啡的标识,因此可用于作主键。第二个列叫 SUP_ID,用于保存咖啡供应商标识;其 SQL 数据类型为 INTEGER。第 3 列叫 PRICE,因为它需要保存带小数的十进制数,因此它的 SQL 类型为 FLOAT。(注意,通常钱的 SQL 类型为 DECIMAL 或 NUMERIC,但在不同 DBMSs 间存在差异,为了避免于老版本的 JDBC 的不兼容性在本教程我们采用更标准的 FLOAT 类型)SALES 列的 SQL 类型为 INTEGER,其值为本星期所卖咖啡的磅数。最后一列,TOTAL 的 SQL 类型为 INTEGER,保存了迄今为止所卖咖啡的总磅数。
数据库里的第二个表 SUPPLIERS,保存了每个供应商的信息:
SUP_ID SUP_NAME STREET CITY STATE ZIP
101 Acme, Inc. 99 Market Street Groundsville CA 95199
49 Superior Coffee 1 Party Place Mendocino CA 95460
150 The High Ground 100 Coffee Lane Meadows CA 93966
COFFEES 跟 SUPPLIERS 都包含列 SUP_ID,它意味着可以用 SELECT 语句从这两张表中取得有关信息。列 SUP_ID 是 SUPPLIERS 表的主键,用于唯一识别每个咖啡供应商。在 COFFEES 表中,SUP_ID 列被称外键。注意每个 SUP_ID 值在 SUPPLIERS 表里只出现一次;这对主键是必须的。在 COFFEES 表里,它作为外键,显然它可以有重复的 SUP_ID 值,因为同一供应商可以提供很多种的咖啡。在本节的最后,你将看见如何在 SELECT 语句中使用主键及外键的一个例子。
下面的 SQL 语句用于创建 COFFEES 表。列由列名跟空格跟 SQL 类型组成。列(包括列名及其 SQL 类型)跟下一个之间用逗号分隔。VARCHAR 类型创建定义了最大长度, 因此它需要有一个参数来表示最大长度。参数必须在类型后面的括号内。SQL 语句如下,列 COF_NAME 的长度 被限定为不得超过 32 个字符:
CREATE TABLE COFFEES
(COF_NAME VARCHAR(32),
SUP_ID INTEGER,
PRICE FLOAT,
SALES INTEGER,
TOTAL INTEGER)
这些代码不带 DBMS 语句结束符, 因为每个 DBMS 都可能不同。例如, Oracle 使用一个分号 (;) 作为语句的结束,而 Sybase 使用 go。你所使用的驱动程序会自动提供合适的语句结束符,因此你无须把它包括在你的 JDBC 代码中。
另外,我们应该指出的的是 SQL 语句的格式。在 CREATE TABLE 语句中,关键字采用大写字符,并且每个项目都另起一行。SQL 并没有此要求;仅仅是为了更容易阅读。SQL 标准是不区分关键词的大小写的, 因此,如下例中的 SELECT 语句可以有多种写法。因此下面两个不同写法的语句对 SQL 来说是一样的。
SELECT First_Name, Last_Name
FROM Employees
WHERE Last_Name LIKE "Washington"
select First_Name, Last_Name from Employees where

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Diffusion can not only imitate better, but also "create". The diffusion model (DiffusionModel) is an image generation model. Compared with the well-known algorithms such as GAN and VAE in the field of AI, the diffusion model takes a different approach. Its main idea is a process of first adding noise to the image and then gradually denoising it. How to denoise and restore the original image is the core part of the algorithm. The final algorithm is able to generate an image from a random noisy image. In recent years, the phenomenal growth of generative AI has enabled many exciting applications in text-to-image generation, video generation, and more. The basic principle behind these generative tools is the concept of diffusion, a special sampling mechanism that overcomes the limitations of previous methods.

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Kimi: In just one sentence, in just ten seconds, a PPT will be ready. PPT is so annoying! To hold a meeting, you need to have a PPT; to write a weekly report, you need to have a PPT; to make an investment, you need to show a PPT; even when you accuse someone of cheating, you have to send a PPT. College is more like studying a PPT major. You watch PPT in class and do PPT after class. Perhaps, when Dennis Austin invented PPT 37 years ago, he did not expect that one day PPT would become so widespread. Talking about our hard experience of making PPT brings tears to our eyes. "It took three months to make a PPT of more than 20 pages, and I revised it dozens of times. I felt like vomiting when I saw the PPT." "At my peak, I did five PPTs a day, and even my breathing was PPT." If you have an impromptu meeting, you should do it

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

In the early morning of June 20th, Beijing time, CVPR2024, the top international computer vision conference held in Seattle, officially announced the best paper and other awards. This year, a total of 10 papers won awards, including 2 best papers and 2 best student papers. In addition, there were 2 best paper nominations and 4 best student paper nominations. The top conference in the field of computer vision (CV) is CVPR, which attracts a large number of research institutions and universities every year. According to statistics, a total of 11,532 papers were submitted this year, and 2,719 were accepted, with an acceptance rate of 23.6%. According to Georgia Institute of Technology’s statistical analysis of CVPR2024 data, from the perspective of research topics, the largest number of papers is image and video synthesis and generation (Imageandvideosyn

Title: A must-read for technical beginners: Difficulty analysis of C language and Python, requiring specific code examples In today's digital age, programming technology has become an increasingly important ability. Whether you want to work in fields such as software development, data analysis, artificial intelligence, or just learn programming out of interest, choosing a suitable programming language is the first step. Among many programming languages, C language and Python are two widely used programming languages, each with its own characteristics. This article will analyze the difficulty levels of C language and Python

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.
