Moni.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Scanner;
class Direction {
int x_offset;
int y_offset;
public Direction(int x, int y) {
x_offset = x;
y_offset = y;
}
}
class Position {
int x, y;
int time;
}
class Record {
int car_id;
int x, y;
public Record(int id, int x, int y){
this.car_id = id;
this.x = x;
this.y = y;
}
public void print() {
System.out.println(car_id + " " + x + " " + y);
System.out.println("hello");
}
}
class Car {
int num;
String str;
int totalTime;
int idx;
Position[] pos;
public Car(int num, String str, int totalTime){
this.num = num;
this.str = str;
this.totalTime = totalTime;
pos = new Position[15];
}
public void setPos(int idx, int x, int y, int time) {
pos[idx] = new Position();
pos[idx].x = x;
pos[idx].y = y;
totalTime += time;
pos[idx].time = totalTime;
}
}
public class Moni {
static String str;
static Scanner cin;
static BufferedInputStream bis;
static Direction[] directions = new Direction[4];
static int tmp;
static ArrayList[] records = new ArrayList[300];
public static void main(String[] args) {
directions[0] = new Direction(0, 1);
directions[1] = new Direction(0, -1);
directions[2] = new Direction(-1, 0);
directions[3] = new Direction(1, 0);
Car[] car = new Car[200000];
for(int i = 0; i < 300; i++) {
records[i] = new ArrayList<Record>();
}
try {
bis = new BufferedInputStream(new FileInputStream("car.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
cin = new Scanner(bis);
long startTime=System.currentTimeMillis();
for(int i = 0; i < 200000; i++) {
str = cin.nextLine();
if (str.endsWith("0")) {
str = str.substring(0, 7);
} else {
str = str.substring(8);
}
car[i] = new Car(i, str, 0);
tmp = (int) (Math.random() * 78 + 30);
car[i].pos[0] = new Position();
car[i].setPos(0, (int) (Math.random() * 40), (int) (Math.random() * 40), tmp);
}
for(int i = 0; i < 200; i++) {
int dir;
int idx = 1;
while(true){
tmp = (int) (Math.random() * 78 + 30);
if(tmp < 36) dir = (int) (Math.random() * 2 + 2);
else if (tmp > 90) dir = (int) (Math.random() * 2);
else dir = (int) (Math.random() * 4);
int dirx = car[i].pos[idx - 1].x + directions[dir].x_offset;
int diry = car[i].pos[idx - 1].y + directions[dir].y_offset;
if(dirx < 0 || dirx >= 40) dirx = car[i].pos[idx - 1].x - directions[dir].x_offset;
if(diry < 0 || diry >= 40) diry = car[i].pos[idx - 1].y - directions[dir].y_offset;
car[i].setPos(idx, dirx, diry, tmp);
car[i].idx = idx;
idx ++;
if(car[i].totalTime >= 300) break;
records[car[i].totalTime].add(new Record(i, dirx, diry));
}
}
int id = 0;
record_dataDAO rDao = new record_dataDAO();
record_data rd = new record_data();
for(int i = 0; i < 300; i++) {
for(int j = 0; j < records[i].size(); j++) {
Record record = (Record) records[i].get(j);
rd.setId(id);
rd.setTime(i);
rd.setCar_str(car[record.car_id].str);
rd.setPos_x(record.x);
rd.setPos_y(record.y);
rDao.insert1(rd);
id++;
}
System.out.println("第" + id + "行 已经instert");
}
long endTime=System.currentTimeMillis();
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
}
}
BaseConection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BaseConnection {
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.
getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeRes(ResultSet rs,PreparedStatement ps,Connection conn){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void closeRes(PreparedStatement ps,Connection conn){
try {
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection conn =BaseConnection.getConnection();
System.out.println(conn);
}
}
record_data.java
public class record_data {
int id;
int time;
String car_str;
int pos_x;
int pos_y;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String getCar_str() {
return car_str;
}
public void setCar_str(String car_str) {
this.car_str = car_str;
}
public int getPos_x() {
return pos_x;
}
public void setPos_x(int pos_x) {
this.pos_x = pos_x;
}
public int getPos_y() {
return pos_y;
}
public void setPos_y(int pos_y) {
this.pos_y = pos_y;
}
}
record_dataDAO.java
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.ArrayList
public class record_dataDAO {
public ArrayList<record_data> getList() {
ArrayList<record_data> ar = new ArrayList<record_data>()
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
ResultSet rs = null
try {
String sql = "select * from record_data"
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
while (rs.next()) {
record_data ne = new record_data()
ne.setId(rs.getInt("id"))
ne.setTime(rs.getInt("time"))
ne.setCar_str(rs.getString("car_str"))
ne.setPos_x(rs.getInt("pos_x"))
ne.setPos_y(rs.getInt("pos_y"))
ar.add(ne)
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(rs, ps, conn)
}
return ar
}
public ArrayList<record_data> getListAll() {
ArrayList<record_data> ar = new ArrayList<record_data>()
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
ResultSet rs = null
try {
String sql = "select * from record_data "
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
while (rs.next()) {
record_data ne = new record_data()
ne.setId(rs.getInt("id"))
ne.setTime(rs.getInt("time"))
ne.setCar_str(rs.getString("car_str"))
ne.setPos_x(rs.getInt("pos_x"))
ne.setPos_y(rs.getInt("pos_y"))
ar.add(ne)
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(rs, ps, conn)
}
return ar
}
public void insert(record_data ne) { // id自增的Insert
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
String sql = "insert into record_data (time,car_str,pos_x,pos_y) " + "values (" + ne.getTime() + ",'"
+ ne.getCar_str() + "'," + ne.getPos_x() + "," + ne.getPos_y() + ")"
try {
ps = conn.prepareStatement(sql)
int a = ps.executeUpdate()
if (a > 0) {
System.out.println("Yes")
} else {
System.out.println("Wrong")
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(ps, conn)
}
}
public boolean insert1(record_data ne) { // 自己设置id的Instert,mysql上勾选AI也可以
boolean b = false
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
String sql = "insert into record_data (id,time,car_str,pos_x,pos_y) " + "values (?,?,?,?,?)"
try {
ps = conn.prepareStatement(sql)
ps.setInt(1, ne.getId())
ps.setInt(2, ne.getTime())
ps.setString(3, ne.getCar_str())
ps.setInt(4, ne.getPos_x())
ps.setLong(5, ne.getPos_y())
int a = ps.executeUpdate()
if (a > 0) {
b = true
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(ps, conn)
}
return b
}
public boolean update(record_data ne) {
boolean b = false
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
String sql = "update record_data set time = ?,car_str = ?,pos_x = ?,pos_y = ? " + " where id = ? "
try {
ps = conn.prepareStatement(sql)
ps.setInt(1, ne.getTime())
ps.setString(2, ne.getCar_str())
ps.setInt(3, ne.getPos_x())
ps.setInt(4, ne.getPos_y())
ps.setInt(5, ne.getId())
int a = ps.executeUpdate()
if (a > 0) {
b = true
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(ps, conn)
}
return b
}
public boolean delete(int id) {
boolean b = false
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
String sql = "delete from record_data where id = ?"
try {
ps = conn.prepareStatement(sql)
ps.setInt(1, id)
int a = ps.executeUpdate()
if (a > 0) {
b = true
}
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(ps, conn)
}
return b
}
public void deleteAll() {
Connection conn = BaseConnection.getConnection()
PreparedStatement ps = null
String sql = "delete from record_data"
try {
ps = conn.prepareStatement(sql)
ps.executeUpdate()
} catch (Exception e) {
e.printStackTrace()
} finally {
BaseConnection.closeRes(ps, conn)
}
}
// Test
public static void main(String[] args) {
record_data ne = new record_data()
record_dataDAO neda = new record_dataDAO()
ne.setTime(100)
ne.setCar_str("A100034")
ne.setPos_x(20)
ne.setPos_y(30)
ne.setId(1)
// neda.insert1(ne)
// neda.insert(ne)
// neda.update(ne)
// neda.delete(2)
ArrayList<record_data> ar = new record_dataDAO().getListAll()
for (record_data net : ar) {
System.out.println(net.getId() + " " + net.getTime() + " " + net.getCar_str() + " " + net.getPos_x() + " "
+ net.getPos_y())
}
}
}