日期:2014-05-20  浏览次数:20831 次

java连接数据库问题 在线等!!!!!!!!!!!!!!!
Java code
package first;
import java.sql.*;
public class Demo123 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Connection ct=null;
        Statement sm=null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            ct=DriverManager.getConnection("jdbc:odbc:mytext");
            sm=ct.createStatement();
            int i=sm.executeUpdate("delect from 123 where name='aaa'");
            if(i==1){
                System.out.print("添加成功");
                
            }
            else
            {
                System.out.print("添加失败");
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        finally
        {
            try {
                if(sm!=null)
                {
                    sm.close();
                }
                if(ct!=null)
                {
                    ct.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}



提示这样的错误
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]关键字 'from' 附近有语法错误。
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
at first.Demo123.main(Demo123.java:16)


------解决方案--------------------
探讨
delete我改了 还是那个错误

------解决方案--------------------
"into" not "inte"
------解决方案--------------------
只能说桥接法连接数据库是有bug的,改用网络法吧(导入sqljdbc.jar文件)
------解决方案--------------------
ct=DriverManager.getConnection("jdbc:odbc:mytext");
改成
ct=DriverManager.getConnection("jdbc:odbc:mytext","","");

------解决方案--------------------
楼主 你的SQL错误了

delect from 123 where name='aaa'

改成

delete from 123 where name='aaa'

delete写错了