package MySQLExercise;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class ExerciseSecond {
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://127.0.0.1:3306/javademo";
public static final String DBUSER="root";
public static final String DBPASS="123";
public static void main(String args[])throws Exception {
Connection conn = null;
Statement stmt = null;
BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
String str=null;
StringBuffer sql=new StringBuffer();
sql.append("create table ");
System.out.print("Please input table name : ");
str=buff.readLine();
sql.append(str);
sql.append("(");
System.out.println("Please input field,press Enter to end: ");
while(true){
System.out.print("Please input filed name : ");
str=buff.readLine();
if("".equals(str)) break;
sql.append(str);
}
sql.append(",");
int len=sql.length();
sql.delete(len-1,len);
sql.append(")");
System.out.println(sql);
Class.forName(DBDRIVER);
conn= DriverManager.getConnection(DBURL,DBUSER,DBPASS);
stmt=conn.createStatement();
stmt.execute(sql+"");
System.out.println("New table created.");
}
}
转载于:https://www.cnblogs.com/ssMellon/p/6508113.html