注册 登录
编程论坛 J2EE论坛

JDBC问题

亮剑 发布于 2007-07-20 11:16, 573 次点击


public ArrayList findUserByAll() {
        String sql1 = \"select id,user_name,password from user\";
        String sql2 = \"select b.actor_name,c.droit_name\"
            + \"from user as a,actor as b,droit as c,user_actor_droit as d \"
            + \"where b.id = d.actor_id and c.id = d.droit_id and a.id = ?\";
        try {
            ps = con.prepareStatement(sql1);
            ResultSet result1 = ps.executeQuery();
            ArrayList<User> users = new ArrayList<User>();
            while(result1.next()){
                User user = new User();
                user.setId(result1.getString(1));
                user.setName(result1.getString(2));
                user.setPassword(result1.getString(3));
                Actor actor = new Actor();
                actor.setUser(user);
                PreparedStatement ps1 = con.prepareStatement(sql2);
                ps1.setString(1, user.getId());
                ResultSet result2 = ps1.executeQuery();

                actor.setName(result2.getString(1));
                ArrayList droits = new ArrayList();
                while(result2.next()){
                    Droit droit = new Droit();
                    droit.setName(result2.getString(2));
                    droits.add(droit);
                }
                user.setActor(actor);
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }



=========================为什么会有下面的错误啊 我直接把SQL 语句复制到数据库端执行没有错误=======

        
java.sql.SQLException: 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 'as a,actor as b,droit as c,user_actor_droit as d where b.id = d.actor_id and c.i' at line 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)
    at com.chen.dao.UserDao.findUserByAll(UserDao.java:91)
    at com.chen.dao.test.UserTest.testfindUserByAll(UserTest.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

[此贴子已经被作者于2007-7-20 13:51:01编辑过]

3 回复
#2
lgdcky2007-07-20 16:29
You have an error in your SQL syntax!
#3
野蛮女人2007-07-20 23:54
小兄弟
String sql2 = "select b.actor_name,c.droit_name"《这是不是少了个空格啊》
+ "from user as a,actor as b,droit as c,user_actor_droit as d "
+ "where b.id = d.actor_id and c.id = d.droit_id and a.id = ?";
#4
野蛮女人2007-07-20 23:54
1