A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赢无翳 中级黑马   /  2016-9-30 15:06  /  1189 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

不知道这里提交能不能显示行号 我是第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和老师的姓名都复制到点名表中相应的字段中去

1 个回复

倒序浏览
这绝对不止第多少行报错的事.
本来我觉得应该只是哪的小问题,结果仔细一看,你这都知道主外键,怎么还写配置参数,写statement语句而不用连接池,QueryRunner之类的东西,加载驱动类也不用Class.forName(),还要注册驱动?建表语句不写表名......三张表,全部复制,直接遍历结果集添加就完了,添加外键也不写筛选条件加外键搞多表连接干嘛?简单测个功能实现你要搞多少样本......
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马