2016-04-11 14 views
0

sütunlar ile bir tablo oluşturma Ben zaten kullanılarak yok eğer İsterdim MySQL son derece yeni ve ben istenen sütunları ile MySQL tablo oluşturma ile birlikte nasıl gidebiliriz merak ediyordum Java.Java MySQL

Visual of what I'd like

+1

Başlangıç ​​ile [JDBC Veritabanı Erişim] (http://docs.oracle.com/javase/tutorial/jdbc/) ve ([TABLO sözdizimi CREATE] http://dev.mysql.com/doc/ refman/5.7/tr/create-table.html) – MadProgrammer

cevap

0

Aşağıdaki kod tam olarak ne istediğinizi yapmak yazılır. Java ile bir mySQL sunucusuna bağlanmak için JDBC mySQL sürücüsünü kullanmalısınız. Bu yardımcı olur umarım.

//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

sen yürütmek her kod satırının ne anlatmak ister misin? Bunu anlaman – MrExporting24

+0

bazı fazla yorum eklendi. Java mysql konektör kütüphanesi hakkında daha fazla bilgi edinebilir ve daha fazlasını bulabilirsiniz. Daha fazlasını anlamanıza yardımcı olacak. İşte –

+1

gitmek http://www.tutorialspoint.com/jdbc/jdbc-create-tables.htm – kakurala