为登录帐户L1创建管理综合实验一创建的数据库(以学号为数据库名称)数据库用户user1T-SQL语句
时间: 2024-03-30 10:35:40 浏览: 12
很抱歉,我并不是一个可以直接操作数据库的程序,无法为您提供创建数据库的具体 T-SQL 语句。但是,您可以参考下面的 T-SQL 语句创建一个新的数据库用户,并为其赋予相应的权限:
```
CREATE LOGIN user1 WITH PASSWORD = 'password';
CREATE USER user1 FOR LOGIN user1;
USE L1;
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO user1;
```
其中,`user1` 为您创建的新用户的名称,`password` 为该用户的密码,`L1` 为您的目标数据库的名称。通过以上语句,您可以为 `user1` 用户授予 `dbo` 模式下的 `SELECT`、`INSERT`、`UPDATE` 和 `DELETE` 操作权限。请根据您的具体需求进行相应的修改。
相关问题
用java连接数据库写一个有登录页面进入商品库存管理(需要增删改查中文注释)
好的,我可以为您提供一个简单的Java登录页面,并且演示如何用Java连接MySQL数据库并实现商品库存管理。以下是详细步骤:
1. 创建Java项目并添加MySQL连接器
首先,您需要创建一个Java项目,在该项目中添加 MySQL 连接器。您可以在此处下载最新版本的 MySQL Connector/J:https://dev.mysql.com/downloads/connector/j/
将下载的 JAR 文件添加到您的项目的classpath中。
2. 创建登录页面
在您的 Java 项目中,创建一个登录页面。您可以使用Swing或JavaFX创建登录页面。
下面是一个简单的Swing登录页面示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame implements ActionListener {
JLabel l1, l2, l3;
JTextField tf1;
JPasswordField pf2;
JButton b1, b2;
Login() {
setTitle("登录");
setSize(300, 200);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("用户名:");
l1.setBounds(40, 20, 100, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(150, 20, 100, 20);
add(tf1);
l2 = new JLabel("密码:");
l2.setBounds(40, 50, 100, 20);
add(l2);
pf2 = new JPasswordField();
pf2.setBounds(150, 50, 100, 20);
add(pf2);
b1 = new JButton("登录");
b1.setBounds(40, 100, 80, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("取消");
b2.setBounds(150, 100, 80, 30);
b2.addActionListener(this);
add(b2);
}
public void actionPerformed(ActionEvent ae) {
String uname = tf1.getText();
String pass = pf2.getText();
if (ae.getSource() == b1) {
if (uname.equals("admin") && pass.equals("admin")) {
JOptionPane.showMessageDialog(this, "登录成功");
dispose();
new InventoryManagement();
} else {
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
} else {
dispose();
}
}
public static void main(String[] args) {
new Login().setVisible(true);
}
}
```
3. 创建商品库存管理页面
创建一个新的 Java 类,名为 InventoryManagement,该类将显示商品库存管理界面。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InventoryManagement extends JFrame implements ActionListener {
JLabel l1, l2, l3, l4, l5, l6;
JTextField tf1, tf2, tf3, tf4, tf5;
JButton b1, b2, b3, b4;
JTextArea ta;
InventoryManagement() {
setTitle("商品库存管理");
setSize(500, 400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("商品编号:");
l1.setBounds(20, 20, 100, 20);
add(l1);
tf1 = new JTextField();
tf1.setBounds(120, 20, 100, 20);
add(tf1);
l2 = new JLabel("商品名称:");
l2.setBounds(20, 50, 100, 20);
add(l2);
tf2 = new JTextField();
tf2.setBounds(120, 50, 100, 20);
add(tf2);
l3 = new JLabel("商品规格:");
l3.setBounds(20, 80, 100, 20);
add(l3);
tf3 = new JTextField();
tf3.setBounds(120, 80, 100, 20);
add(tf3);
l4 = new JLabel("商品单价:");
l4.setBounds(20, 110, 100, 20);
add(l4);
tf4 = new JTextField();
tf4.setBounds(120, 110, 100, 20);
add(tf4);
l5 = new JLabel("商品数量:");
l5.setBounds(20, 140, 100, 20);
add(l5);
tf5 = new JTextField();
tf5.setBounds(120, 140, 100, 20);
add(tf5);
b1 = new JButton("新增");
b1.setBounds(20, 180, 80, 30);
b1.addActionListener(this);
add(b1);
b2 = new JButton("查询");
b2.setBounds(110, 180, 80, 30);
b2.addActionListener(this);
add(b2);
b3 = new JButton("修改");
b3.setBounds(200, 180, 80, 30);
b3.addActionListener(this);
add(b3);
b4 = new JButton("删除");
b4.setBounds(290, 180, 80, 30);
b4.addActionListener(this);
add(b4);
ta = new JTextArea();
ta.setBounds(20, 220, 440, 120);
add(ta);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String cmd = ae.getActionCommand();
if (cmd.equals("新增")) {
addProduct();
} else if (cmd.equals("查询")) {
searchProduct();
} else if (cmd.equals("修改")) {
updateProduct();
} else if (cmd.equals("删除")) {
deleteProduct();
}
}
private void addProduct() {
String id = tf1.getText();
String name = tf2.getText();
String spec = tf3.getText();
double price = Double.parseDouble(tf4.getText());
int quantity = Integer.parseInt(tf5.getText());
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行插入语句
PreparedStatement ps = conn.prepareStatement("INSERT INTO products (id, name, spec, price, quantity) VALUES (?, ?, ?, ?, ?)");
ps.setString(1, id);
ps.setString(2, name);
ps.setString(3, spec);
ps.setDouble(4, price);
ps.setInt(5, quantity);
ps.executeUpdate();
ta.setText("新增商品成功");
} catch (Exception e) {
e.printStackTrace();
ta.setText("新增商品失败:" + e.getMessage());
}
}
private void searchProduct() {
String id = tf1.getText();
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行查询语句
PreparedStatement ps = conn.prepareStatement("SELECT * FROM products WHERE id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
tf2.setText(rs.getString("name"));
tf3.setText(rs.getString("spec"));
tf4.setText(Double.toString(rs.getDouble("price")));
tf5.setText(Integer.toString(rs.getInt("quantity")));
ta.setText("");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("查询商品失败:" + e.getMessage());
}
}
private void updateProduct() {
String id = tf1.getText();
String name = tf2.getText();
String spec = tf3.getText();
double price = Double.parseDouble(tf4.getText());
int quantity = Integer.parseInt(tf5.getText());
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行更新语句
PreparedStatement ps = conn.prepareStatement("UPDATE products SET name = ?, spec = ?, price = ?, quantity = ? WHERE id = ?");
ps.setString(1, name);
ps.setString(2, spec);
ps.setDouble(3, price);
ps.setInt(4, quantity);
ps.setString(5, id);
int result = ps.executeUpdate();
if (result > 0) {
ta.setText("修改商品成功");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("修改商品失败:" + e.getMessage());
}
}
private void deleteProduct() {
String id = tf1.getText();
try {
// 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root", "password");
// 执行删除语句
PreparedStatement ps = conn.prepareStatement("DELETE FROM products WHERE id = ?");
ps.setString(1, id);
int result = ps.executeUpdate();
if (result > 0) {
ta.setText("删除商品成功");
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
} else {
ta.setText("没有找到编号为 " + id + " 的商品");
}
} catch (Exception e) {
e.printStackTrace();
ta.setText("删除商品失败:" + e.getMessage());
}
}
public static void main(String[] args) {
new InventoryManagement();
}
}
```
4. 创建MySQL数据库
在您的MySQL数据库中,创建一个名为“inventory”的新数据库,并创建一个名为“products”的新表,该表包含以下列:
- id:VARCHAR
- name:VARCHAR
- spec:VARCHAR
- price:DOUBLE
- quantity:INT
```sql
CREATE DATABASE inventory;
USE inventory;
CREATE TABLE products (
id VARCHAR(10) PRIMARY KEY,
name VARCHAR(50),
spec VARCHAR(50),
price DOUBLE,
quantity INT
);
```
现在您可以编译并运行您的Java程序,以查看它是否可以连接到MySQL数据库并实现商品库存管理了。
用swing组件写出一个完整的酒店管理系统,并连接数据库
好的,下面是一个使用Swing组件编写的完整的酒店管理系统,并连接到数据库的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class HotelManagementSystem extends JFrame implements ActionListener {
JMenuBar mb;
JMenu m1, m2;
JMenuItem i1, i2, i3, i4;
JLabel l1, l2;
HotelManagementSystem() {
mb = new JMenuBar();
add(mb);
m1 = new JMenu("管理");
mb.add(m1);
m2 = new JMenu("帮助");
mb.add(m2);
i1 = new JMenuItem("添加客房");
i1.addActionListener(this);
m1.add(i1);
i2 = new JMenuItem("客房列表");
i2.addActionListener(this);
m1.add(i2);
i3 = new JMenuItem("删除客房");
i3.addActionListener(this);
m1.add(i3);
i4 = new JMenuItem("退出");
i4.addActionListener(this);
m1.add(i4);
l1 = new JLabel("欢迎来到酒店管理系统");
l1.setBounds(200, 50, 400, 30);
l1.setForeground(Color.BLUE);
l1.setFont(new Font("Tahoma", Font.PLAIN, 30));
add(l1);
l2 = new JLabel();
l2.setBounds(0, 0, 1000, 750);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("hotelmanagementsystem/icons/second.jpg"));
Image i2 = i1.getImage().getScaledInstance(1000, 750, Image.SCALE_DEFAULT);
ImageIcon i3 = new ImageIcon(i2);
l2.setIcon(i3);
add(l2);
setLayout(null);
setBounds(0, 0, 1000, 750);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("添加客房")) {
new AddRoom().setVisible(true);
} else if (ae.getActionCommand().equals("客房列表")) {
new Room().setVisible(true);
} else if (ae.getActionCommand().equals("删除客房")) {
new RemoveRoom().setVisible(true);
} else if (ae.getActionCommand().equals("退出")) {
System.exit(0);
}
}
public static void main(String[] args) {
new HotelManagementSystem().setVisible(true);
}
}
class AddRoom extends JFrame implements ActionListener {
JTextField t1, t2, t3, t4, t5;
JComboBox c1, c2, c3;
JButton b1, b2;
AddRoom() {
JLabel l1 = new JLabel("客房号");
l1.setBounds(20, 30, 100, 20);
add(l1);
t1 = new JTextField();
t1.setBounds(130, 30, 150, 20);
add(t1);
JLabel l2 = new JLabel("客房类型");
l2.setBounds(20, 80, 100, 20);
add(l2);
c1 = new JComboBox(new String[] { "单人间", "双人间", "套房" });
c1.setBounds(130, 80, 150, 20);
add(c1);
JLabel l3 = new JLabel("客房价格");
l3.setBounds(20