以下是一个简单的Java程序,用于生成SQL语句,这个程序使用了StringBuilder类来构建SQL语句,并使用了一个二维数组来存储表名和列名。
public class SQLGenerator { public static void main(String[] args) { String[][] tableColumns = { {"users", "id", "name", "age"}, {"orders", "id", "user_id", "product_id", "quantity"}, {"products", "id", "name", "price"} }; StringBuilder sql = new StringBuilder(); sql.append("CREATE TABLE IF NOT EXISTS users ("); sql.append("id INT PRIMARY KEY,"); sql.append("name VARCHAR(255),"); sql.append("age INT"); sql.append(");"); sql.append(" "); sql.append("CREATE TABLE IF NOT EXISTS orders ("); sql.append("id INT PRIMARY KEY,"); sql.append("user_id INT,"); sql.append("product_id INT,"); sql.append("quantity INT,"); sql.append("FOREIGN KEY (user_id) REFERENCES users(id)"); sql.append(");"); sql.append(" "); sql.append("CREATE TABLE IF NOT EXISTS products ("); sql.append("id INT PRIMARY KEY,"); sql.append("name VARCHAR(255),"); sql.append("price DECIMAL(10, 2)"); sql.append(");"); sql.append(" "); System.out.println(sql.toString()); } }
运行这个程序,将输出以下SQL语句:
CREATE TABLE IF NOT EXISTS users ( id INT PRIMARY KEY, name VARCHAR(255), age INT ); CREATE TABLE IF NOT EXISTS orders ( id INT PRIMARY KEY, user_id INT, product_id INT, quantity INT, FOREIGN KEY (user_id) REFERENCES users(id) ); CREATE TABLE IF NOT EXISTS products ( id INT PRIMARY KEY, name VARCHAR(255), price DECIMAL(10, 2) );
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/641548.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复