代码报错?????

代码报错?????

package com.imooc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class Demo {
       Connection conn=null;
       public static void main(String[] args) {
       //运行之前需要创建数据库和表,参考test.sql文件
       Demo d= new Demo();
       Map param=new HashMap();
       param.put("id",5);
       param.put("name","amy");
       param.put("password","123456");
       d.insert(param);
       d.getAll();
       d.close();
       }
	public void createDb() {
		String url="jdbc:mysql:///test1?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8";
		String name="com.mysql.cj.jdbc.Driver";
		String user="root";
		String  password="1234";
		try {
			Class.forName(name);//指定连接类型
			conn=DriverManager.getConnection(url,user,password);
			//pst=conn.preparedStatement(sql);//准备执行语句
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public int insert(Map param) {
		this.createDb();
		int i=0;
		String sql="INSERT INTO user(id,username,password) VALUSE(?,?,?)";
		PreparedStatement pstmt=null;
		try {
			pstmt=this.conn.prepareStatement(sql);
			pstmt.setString(1, (String)param.get("id"));
			pstmt.setString(2, (String)param.get("name"));
		    pstmt.setString(3, (String)param.get("password"));
		    i=pstmt.executeUpdate();
		    pstmt.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return i;
	}
		
	public void getAll() {
		String sql="select * form user";
		PreparedStatement pstmt=null;
		try {
			pstmt=this.conn.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			int col=rs.getMetaData().getColumnCount();
			System.out.println("=========================");
			while(rs.next()) {
				for(int i=1;i<=col;i++) {
					System.out.print(rs.getString(i)+"\t");
					if((i==2)&&(rs.getString(i).length()<8)) {
						System.out.println("\t");
						
					}
				}
				System.out.println("");
			}
			System.out.println("=========================");
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
		public void close() {
		
	}
}

**********************************************************

package com.imooc.test;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DemoTest1 {
    public static Demo d= new Demo();
	@Before
	public void setUp() throws Exception {
		
	}
	@Before
	public void testCreateDb() {
	      d.createDb();
	}

	@Test
	public void testInsert() {
		   Map param=new HashMap();
	       param.put("id",6);
	       param.put("name","larry");
	       param.put("password","123456");
	       d.insert(param);
	}

	@Test
	public void testGetAll() {
		d.getAll();
	}

	@After
	public void testClose() {
		    d.close();
	}

}

报错信息????????????

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form user' at line 1

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)

at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960)

at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1019)

at com.imooc.test.Demo.getAll(Demo.java:61)

at com.imooc.test.DemoTest1.testGetAll(DemoTest1.java:34)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

at com.imooc.test.Demo.insert(Demo.java:44)

at com.imooc.test.DemoTest1.testInsert(DemoTest1.java:29)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

8回答
好帮手慕阿满 2019-03-15 11:15:21

同学可以将项目打包发至( kf@imooc.com)邮箱,请指定是那个方向,即(Java方向),以及同学的用户昵称和uid是什么。

祝:学习愉快~


  • 提问者 慕羲 #1
    好的。。。。。。
    2019-03-15 16:22:25
提问者 慕羲 2019-03-15 08:41:15

还是报错?

http://img1.sycdn.imooc.com//climg/5c8af4a8000119f712340861.jpg

好帮手慕阿满 2019-03-14 15:41:55

同学你好,同学贴出来的有两个报错,一个报错是类型转换异常,Integer不能转换为String类型,在insert()方法中,id应该是int类型的,同学将其变成Integer类型而不是String类型再试试,如:

pstmt.setInteger(1,(Integer)param.get("id"));

另一个是单词拼写错误,from写成了form,如:

http://img1.sycdn.imooc.com//climg/5c8a053c0001163a03990097.jpg

同学修改一下再试试。

如果还有问题,建议同学新开一个问答,在对应的章节下提问。

祝:学习愉快~

  • 提问者 慕羲 #1
    为什么不能把这个项目代码都发过去,这样一点点的找问题,拖得太久了,我都不知道是哪一个章节,这么多的代码?
    2019-03-15 08:31:48
提问者 慕羲 2019-03-13 19:59:11

报错信息?

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

at com.imooc.test.Demo.insert(Demo.java:44)

at com.imooc.test.Demo.main(Demo.java:19)

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'form user' at line 1

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)

at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)

at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960)

at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1019)

at com.imooc.test.Demo.getAll(Demo.java:61)

at com.imooc.test.Demo.main(Demo.java:20)

package com.imooc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class Demo {
       Connection conn=null;
       public static void main(String[] args) {
       //运行之前需要创建数据库和表,参考test.sql文件
       Demo d= new Demo();
       Map param=new HashMap();
       param.put("id",5);
       param.put("name","amy");
       param.put("password","123456");
       d.insert(param);
       d.getAll();
       d.close();
       }
	public void createDb() {
		String url="jdbc:mysql:///test1?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8";
		String name="com.mysql.cj.jdbc.Driver";
		String user="root";
		String  password="1234";
		try {
			Class.forName(name);//指定连接类型
			conn=DriverManager.getConnection(url,user,password);
			//pst=conn.preparedStatement(sql);//准备执行语句
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public int insert(Map param) {
		this.createDb();
		int i=0;
		String sql="INSERT INTO user(id,username,password) VALUSE(?,?,?)";
		PreparedStatement pstmt=null;
		try {
			pstmt=this.conn.prepareStatement(sql);
			pstmt.setString(1, (String)param.get("id"));
			pstmt.setString(2, (String)param.get("name"));
		    pstmt.setString(3, (String)param.get("password"));
		    i=pstmt.executeUpdate();
		    pstmt.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		return i;
	}
		
	public void getAll() {
		String sql="select * form user";
		PreparedStatement pstmt=null;
		try {
			pstmt=this.conn.prepareStatement(sql);
			ResultSet rs=pstmt.executeQuery();
			int col=rs.getMetaData().getColumnCount();
			System.out.println("=========================");
			while(rs.next()) {
				for(int i=1;i<=col;i++) {
					System.out.print(rs.getString(i)+"\t");
					if((i==2)&&(rs.getString(i).length()<8)) {
						System.out.println("\t");
						
					}
				}
				System.out.println("");
			}
			System.out.println("=========================");
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
		public void close() {
		
	}
}


好帮手慕阿满 2019-03-12 19:33:59

同学你好,在上边的代码中,是sql语句错误,from写成了form,同学需要修改一下。另外下边的问题,建议同学将代码贴上来,请将问题描述清楚,如果有多个问题,请使用1,2,3等序号标清楚。建议一个问答解决一个问题,不要对一个问题重复提问,不要在一个问答中解决不同的代码问题。不要多次反复回复,避免顺序混乱,造成遗漏问题。祝:学习愉快~

提问者 慕羲 2019-03-12 16:47:33

http://img1.sycdn.imooc.com//climg/5c8771f000011d4111560731.jpg

 这里有两个类,为什么只导入一个???

 import com.imooc.test.DemoTest1;

import com.imooc.test.CalculatorTest1;

为什么不用import com.imooc.test.*;代替

  • 提问者 慕羲 #1
    3.。。。。。。。。。。。。。。。。
    2019-03-13 19:56:35
提问者 慕羲 2019-03-12 16:33:36

http://img1.sycdn.imooc.com//climg/5c876ebf0001e57005650411.jpg

视频的代码敲完报错???

  • 提问者 慕羲 #1
    2.。。。。。。。。。。。。。。。。
    2019-03-13 19:56:18
提问者 慕羲 2019-03-12 16:23:10

第一个红圈是方法测试的先后实现顺序,教铺里面没有找到这个代码???

第二个红圈是单元测试,一起测试,教铺里面也没有找到代码???

http://img1.sycdn.imooc.com//climg/5c876c6b000183fa14470817.jpg


  • 提问者 慕羲 #1
    10个问题已经用完,只能在这里提问了???
    2019-03-12 16:34:16
  • 提问者 慕羲 #2
    1。。。。。。。。。。。
    2019-03-13 19:56:02
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师