Results 1 to 7 of 7

Thread: MySQLSyntaxErrorException

  1. #1
    Join Date
    Oct 2012
    Location
    Munich, Germany
    Posts
    5

    Default MySQLSyntaxErrorException

    Hi there!
    Im relatively new to spring and Database-Access with it, so I don't know where this error.message comes from...

    Code:
    SCHWERWIEGEND: Servlet.service() for servlet persons threw exception
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'persons.persons' doesn't exist
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    Here's the code-snippet, where it is caused:
    Code:
    	public List<Persons> getPersons() {
    		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    		String SQL = "SELECT * FROM persons;";
    		List<Persons> person = jdbcTemplate.query(SQL, new PersonsMapper()); 
    		return person;
    	}
    Its the row
    Code:
    List<Persons> person = jdbcTemplate.query(SQL, new PersonsMapper());
    is there something wrong with my SQL-Statement, or is it the PersonsMapper?

    the PersonsMapper-class:
    public class PersonsMapper implements RowMapper<Persons> {
    Code:
    	public Persons mapRow(ResultSet rs, int rowCount) throws SQLException {
    		// TODO Auto-generated method stub
    		Persons person = new Persons();
    		person.setId(rs.getInt("id"));
    		person.setFirst_name(rs.getString("first_name"));
    		person.setLast_name(rs.getString("last_name"));
    		person.setDate_of_birth(rs.getDate("date_of_birth"));
    		return person;
    	}
    }
    the controller-class looks like this:
    Code:
    @RequestMapping({"/","/persons"})
    	public String showHomePage(Map<String, Object> model) {
    		personsDAO = new PersonsDAO();
    		System.out.println("1.1");
    		personsDAO.getDataSource();
    		System.out.println("1.2");
    		model.put("persons", personsDAO.getPersons());
    		System.out.println("1.3");
    		return "persons";
    		
    	}
    and the -servlet.xml looks like this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    	
    	<context:component-scan base-package="lissy.persons"/>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix">
    			<value>/WEB-INF/views/</value>
    		</property>
    		<property name="suffix">
    			<value>.jsp</value>
    		</property>
    	</bean>
    	
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    		<property name="url" value="jdbc:mysql://localhost/persons"></property>
    		<property name="username" value="root"></property>
    		<property name="password" value="MySQL2012pwd"></property>
    	</bean>
    	
    	<bean id="personsDAO" class="lissy.persons.PersonsDAO">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	
    </beans>
    does anybody have an idea what's wrong with this?
    Thanks!!!

  2. #2
    Join Date
    Sep 2012
    Location
    new york
    Posts
    10

    Default

    Well I don't think there is any one to read your code, that is really written in very small font size, but I have an advice for you, if you even, can't remove the syntax error by yourself just left the programming LOLZ. Because syntax error is easy to detect by the coder himself. So, just try.
    .

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Have you read the stacktrace?

    Code:
    Table 'persons.persons' doesn't exist
    Seems pretty clear to me?! You don't have a table named persons (you only have a schema).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Oct 2012
    Location
    Munich, Germany
    Posts
    5

    Default

    Sorry!!! I didn't mention that: This table exists, but it seems he doesn't find it...

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Trust me it doesn't, at least not in the schema/database you are connecting to... Also a schema isn't a table...

    Also your code is wrong (I strongly suggest a read on DI and IoC)... Basically your PersonDAO definition is useless as you are creating a new instance in your controller...
    Last edited by Marten Deinum; Oct 8th, 2012 at 02:38 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Oct 2012
    Location
    Munich, Germany
    Posts
    5

    Default

    Thanks for your hints! I will dive in DI and IoC (I thought I understood this...)..

    As it looks like, you have much more experience with spring than me, could you answer me some questions?
    Code:
    Table 'persons.persons' doesn't exist
    It seems like I haven't referenced this table properly (because I know it exists).. Could you give me some hints how to do this?
    Can you tell me whats wrong with my code?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I also suggest you read up on MySQL...

    As mentioned before

    Quote Originally Posted by mdeinum
    Trust me it doesn't, at least not in the schema/database you are connecting to... Also a schema isn't a table...
    So as stated the table simply doesn't exists... It is either named differently (person?) or it isn't in the schema you are connecting to (persons)... Or the account you are connecting with doesn't have priviliges... This has nothing to do with java it has to do with your server setup.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •