提示用户输入 table 创建的详细信息
Prompting user to enter details for table creation
import java.sql.*;
import java.util.Scanner;
public class DynamicTableCreate {
static String url, user, password;
static {
url="jdbc:mysql://localhost/testing";
user="root";
password="root";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
Statement stmt=null;
Scanner scan=new Scanner(System.in);
System.out.println("Enter table name");
String tname=scan.next();
System.out.println("Enter number of columns");
int cols=scan.nextInt();
System.out.println("Enter column names and data types ");
String [] colnames=new String[5];
String [] datatype=new String[5];
int i=0;
while(i<cols)
{
colnames[i]=scan.next();
datatype[i]=scan.next();
i++;
}
// Create table Trainers (T_Id int primary key, Trainer_name varchar(20), Stream varchar(20));
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
String sql1 = "create table " + tname + "(" + colnames[0] + " " + datatype[0] + " primary key, ";
for(i=1;i<cols;i++)
{
sql1 += colnames[i] + " " + datatype[i] + ", ";
}
sql1+=");";
stmt.executeUpdate(sql1);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if (stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(Exception e)
{
}
}
}
}
我想创建 table,其中用户提供 table 名称、列数、列名及其数据类型,但我的代码抛出以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have
an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ')' at line 1
解决方法是什么?
问题已解决...
唯一要做的改变就是替换
sql1 += colnames[i] + " " + datatype[i] + ", ";
来自
sql1 += ", " + colnames[i] + " " + datatype[i];
import java.sql.*;
import java.util.Scanner;
public class DynamicTableCreate {
static String url, user, password;
static {
url="jdbc:mysql://localhost/testing";
user="root";
password="root";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
Statement stmt=null;
Scanner scan=new Scanner(System.in);
System.out.println("Enter table name");
String tname=scan.next();
System.out.println("Enter number of columns");
int cols=scan.nextInt();
System.out.println("Enter column names and data types ");
String [] colnames=new String[5];
String [] datatype=new String[5];
int i=0;
while(i<cols)
{
colnames[i]=scan.next();
datatype[i]=scan.next();
i++;
}
// Create table Trainers (T_Id int primary key, Trainer_name varchar(20), Stream varchar(20));
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
String sql1 = "create table " + tname + "(" + colnames[0] + " " + datatype[0] + " primary key, ";
for(i=1;i<cols;i++)
{
sql1 += colnames[i] + " " + datatype[i] + ", ";
}
sql1+=");";
stmt.executeUpdate(sql1);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if (stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
catch(Exception e)
{
}
}
}
}
我想创建 table,其中用户提供 table 名称、列数、列名及其数据类型,但我的代码抛出以下错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
解决方法是什么?
问题已解决...
唯一要做的改变就是替换
sql1 += colnames[i] + " " + datatype[i] + ", ";
来自
sql1 += ", " + colnames[i] + " " + datatype[i];