Java之从零开始制作火车售票系统
首先建立数据库,方便能够存储数据
package We;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class TrainDatabase {
Connection conn = null;
String url = "jdbc:derby:TrainDB;create=true"; //url of the DB host
//jdbc:derby://localhost:1527/TrainDB
String username = "pdc"; //your DB username
String password = "pdc"; //your DB password
Statement statement;
ResultSet rs;
public void autoConnectTrainDB() {
try {
conn = DriverManager.getConnection(url, username, password);
statement = conn.createStatement();
statement.executeUpdate("CREATE TABLE TRAIN (STARTPOINT VARCHAR(50), ENDPOINT VARCHAR(50), SEATTYPE VARCHAR(50), NAME VARCHAR(50),SEX VARCHAR(50),TELE FLOAT)"); //Create BOOK table
statement.executeUpdate("INSERT INTO TRAIN VALUES ('SHANG HAI', 'ZHE JIANG', 'Second Seat', 'MIKE','MALE',18036554221) ");
} catch (Throwable ex) {
System.err.println("SQL Exception: " + ex.getMessage());
}
}
/**
* @param args the command line arguments
*/
}
然后主方法能够运行数据库
package We;
import java.sql.ResultSet;
/**
*
* @author shaojiangteng
*/
public class TrainDatabasetest {
public static void main(String[] args) {
TrainDatabase train=new TrainDatabase();
train.autoConnectTrainDB();
}
}