📄 createdatabase.java
字号:
import javax.swing.*;
import java.sql.*;
import java.io.*;
public class CreateDatabase
{
private String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url = "jdbc:microsoft:sqlserver://localhost:1433";
private String createStr = "create database yswkcsj ";
private String path = "";
public CreateDatabase(String user, String password) throws SQLException, ClassNotFoundException
{
try
{
File file = new File("set.txt");
path = file.getAbsolutePath();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
path = path.substring(0, path.length() - 7);
createStr += "on (name = 'yswkcsj_Data',filename = '" +
path + "yswkcsj_Data.mdf', size = 1, maxsize = unlimited, filegrowth = 10%) "
+ "log on ( name = 'yswkcsj_Log',filename = '" + path +
"yswkcsj_Log.ldf', size = 1, maxsize = unlimited, filegrowth = 10%)";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement();
statement.executeUpdate(createStr);
statement.close();
connection.close();
connection = DriverManager.getConnection(url + ";DATABASENAME=yswkcsj", user, password);
statement = connection.createStatement();
createStr = "create table admins (adminID numeric(10, 0) identity primary key, "
+ " adminName varchar(10) not null, adminPassword varchar(15) not null)";
statement.execute(createStr);
statement.execute("insert into admins values('admin', 'admin')");
statement.execute("create table hireTypes (hireTypeID numeric(10, 0) identity primary key, "
+ " adminID numeric(10, 0) references admins not null, foregift money not null, hire money not null,"
+ " canHireNumber int not null )");
statement.execute("create table films (filmID numeric(10, 0) identity(10000, 1) primary key, "
+ " filmName varchar(30) not null, filmType varchar(30) not null,"
+ " CDNumber int not null, players varchar(100), country varchar(50),"
+ " state varchar(4) default '归还' not null, "
+ " hireTimes int default 0 not null, adminID numeric(10, 0) references admins not null,"
+ " inTime datetime default getdate() not null, "
+ " remark varchar(200) )");
statement.execute("create table CDs (CDID numeric(10, 0) identity(20000, 1) primary key, "
+ " CDno varchar(3) not null, filmID numeric(10, 0) references films not null,"
+ " state varchar(4) default '归还' not null, "
+ " isBad bit default 0 not null)");
statement.execute("create table lessees (lesseeID numeric(10, 0) identity(1000, 1) primary key, "
+ " lesseeName varchar(10) not null, phone varchar(15) not null,"
+ " address varchar(50) not null,IDCard varchar(50) not null,"
+ " hadHireNumber int default 0 not null, adminID numeric(10, 0) references admins not null,"
+ " hireTypeID numeric(10, 0) references hireTypes not null )");
statement.execute("create table hireAndBacks (lesseeID numeric(10, 0) references lessees,"
+" CDID numeric(10, 0) references CDs, hireTime datetime default getdate(),"
+ " givebackTime datetime, isback bit default 0 not null primary key(lesseeID, CDID, hireTime))");
statement.close();
connection.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -