2016-04-11 10 views
0

私は非常にMySQLに新しく、好きな列を使ってMySQLでテーブルを作成する方法を知りましたが存在しない場合を使用JavaJavaで列を含むテーブルを作成する

Visual of what I'd like

+1

[JDBCデータベースアクセス](http://docs.oracle.com/javase/tutorial/jdbc/)および[CREATE TABLE構文](http://dev.mysql.com/doc/)から始めてください。 refman/5.7/ja/create-table.html) – MadProgrammer

答えて

0

次のコードは正確にあなたがやりたいように書かれています。 javaを使用してmySQLサーバーに接続するには、JDBC 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

関連する問題