不知道这里提交能不能显示行号 我是第92行报错 就是
statement.executeUpdate(addteacher); 这一行
不知道为啥了 有大神指点一下么?
[Java] 纯文本查看 复制代码 package com.heima.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Test;
import com.mysql.jdbc.Driver;
public class Test09271 {
@Test
public void test() throws SQLException{
String url ="jdbc:mysql://localhost:3306/homework";
String user = "root";
String password = "442586";
DriverManager.registerDriver(new Driver());
Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement();
String sql = "select *from student";
String sql1 = "create table (id int primary key auto_increment,age int,score double,name varchar(20))";
String sql2 = "show databases";
//System.out.println(set.next());
String foreignkeyst = "alter table infro add foreign key(studentid) references student(id)";
//执行语句
statement.executeUpdate(foreignkeyst);
String foreignkeyti = "alter table infro add foreign key(teacher) references teacher(teacher)";
statement.executeUpdate(foreignkeyti);
Random r = new Random();
String [] xing = {"张","王","李","赵","刘","吴","孙"};
String [] ming = {"一","二","三","四","五","六","七"};
List listname = new ArrayList<>();
while (listname.size() <= 50){
int a = r.nextInt(7);
listname.add(new String(xing[a]+ming[a]));
}
List listgrade = new ArrayList<>();
while (listgrade.size() <= 50){
listgrade.add(r.nextInt(3)+1);
}
List listclass = new ArrayList<>();
while (listclass.size() <= 50){
listclass.add(r.nextInt(5)+1);
}
List listage = new ArrayList<>();
while (listage.size() <= 50){
listage.add(r.nextInt(20)+15);
}
for (int i = 0;i <= 50;i++){
String addstudent = "insert into student (name,class,age) values "+"(" +"'"+listname.get(i)+"'" +","+ listgrade.get(i)+""+listclass.get(i)+ ","+listage.get(i)+")";
System.out.println(addstudent);
statement.executeUpdate(addstudent);
}
List listteacher = new ArrayList<>();
listteacher.add("一年一班班主任");
listteacher.add("一年二班班主任");
listteacher.add("一年三班班主任");
listteacher.add("一年四班班主任");
listteacher.add("一年五班班主任");
listteacher.add("二年一班班主任");
listteacher.add("二年二班班主任");
listteacher.add("二年三班班主任");
listteacher.add("二年四班班主任");
listteacher.add("二年五班班主任");
listteacher.add("三年一班班主任");
listteacher.add("三年二班班主任");
listteacher.add("三年三班班主任");
listteacher.add("三年四班班主任");
listteacher.add("三年五班班主任");
for (int i = 0;i < listteacher.size();i++){
String addteacher = "insert into teacher (teacher) values (" + "'" + listteacher.get(i) + "')";
statement.executeUpdate(addteacher);
}
String copystudentteacher = "insert into teacher (studentid) select id from student";
statement.executeUpdate(copystudentteacher);
String copystudent = "insert into infro (studentid) select id from student";
statement.executeUpdate(copystudent);
for (int i = 0;i < 10;i++){
int a = r.nextInt(15);
System.out.println( listteacher.get(a));
String addteacher = "insert into infro (teacher) values (" + "'" + listteacher.get(a) + "')";
System.out.println(addteacher);
statement.executeUpdate(addteacher);
}
String copyteacher = "insert into infro (teacher) select teacher from teacher";
statement.executeUpdate(copyteacher);
for (int i = 0;i <= 50;i++){
int a = r.nextInt(1);
String addinfro = "insert into infro (infro) values (" + a + ")";
statement.executeUpdate(addinfro);
}
// while(set.next()){
// System.out.println(set.getString("name"));
// System.out.println(set.getInt("id"));
// }
// if(set != null){
// set.close();
// set = null;
// }
//
if(statement != null){
statement.close();
statement = null;
}
if(connection != null){
connection.close();
connection = null;
}
}
}
包含姓名 id 一张是老师 包含姓名 还有一张是点名表
我想把学生的id和老师的姓名都复制到点名表中相应的字段中去
|