我对MySQL非常陌生,并且很好奇我怎样才能在MySQL中创建一个具有所需列的表我想如果它不存在使用Java。Java在MySQL中使用列创建表
0
A
回答
0
下面的代码被写入做的正是你想要的。您必须使用JDBC mySQL驱动程序通过java连接到mySQL服务器。希望这可以帮助。
//assuming that you are using JDBC connector for mysql
Class.forName("com.mysql.jdbc.Driver");
//connect to mysql server. We are using a class in the imported java mysql library to establish a connection with the mysql database server
//root is the username, next parameter is the password.
connection = DriverManager.getConnection("jdbc:mysql://localhost/?", "root", "");
//ceeate a statement to be executed in the mysql database server.
Statement statement = connection.createStatement();
//create database if it doesn't exist
//That is, sending a create table mysql command to the database server.
statement.execute("CREATE DATABASE IF NOT EXISTS sheetdata");
//create table if it doesn't exist. This was a example I used. You can change the table definition as you want.
//this will send the create table sql statement to be executed in the mysql server
statement.execute("CREATE TABLE IF NOT EXISTS sheetdata.`saveddata` (\n"
+ " `INDEX` int(10) NOT NULL AUTO_INCREMENT,\n"
+ " `FILENAME` varchar(100) DEFAULT NULL,\n"
+ " `TABLENAME` varchar(100) DEFAULT NULL,\n"
+ " `TIMESTAMP` varchar(50) DEFAULT NULL,\n"
+ " PRIMARY KEY (`INDEX`)\n"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
connection.close();
+0
你介意告诉我执行代码的每一行是干什么的吗? – MrExporting24
+0
增加了一些更多的评论供你理解。你最好搜索并找到更多关于java mysql连接库的信息。这将有助于你了解更多。 –
+1
这里你去http://www.tutorialspoint.com/jdbc/jdbc-create-tables.htm – kakurala
相关问题
- 1. 在mysql中使用java创建表
- 2. 使用列表在MySQL上创建列
- 3. 在JAVA中使用JAVA创建表
- 4. 如何在使用java时在MySQL中创建表格
- 5. Java在MySQL数据库中创建表
- 6. 使用MySQL创建顶级列表
- 7. MySQL使用select,alias列名创建表
- 8. 用java创建列表
- 9. 无法使用Java在MySQL中创建表格
- 10. 从java列表中创建列表
- 11. 在MySQL中创建一个列表
- 12. 在MYSQL的列中创建一个表
- 13. 在Rails中动态创建mysql列表
- 14. 如何在mysql中创建x列表?
- 15. 如何使用python在MYSQL表中创建变量列
- 16. Java阵列列表创建
- 17. 在java中创建对列表
- 18. 在Java中创建对象的列表
- 19. 在BS4中使用findAll创建列表
- 20. 在MYSQL中创建序列
- 21. 使用java在Excel中创建下拉列表
- 22. 使用COM和Java在iTunes中创建播放列表
- 23. 在Java中创建嵌套结构使用对象的列表
- 24. 如何在Java中使用循环创建多个列表?
- 25. 使用列表创建一个MySQL表的列
- 26. 在MySQL中创建表
- 27. 在mySQL中创建空表
- 28. 在JSP中创建MySQL表
- 29. 使用mysql填充mysql创建表
- 30. 在SharePoint中使用列表中的值创建下拉列表
开始与[JDBC数据库访问(http://docs.oracle.com/javase/tutorial/jdbc/)和[CREATE TABLE语法](http://dev.mysql.com/doc/ refman/5.7/en/create-table.html) – MadProgrammer