Java连接到SQL Anywhere 17

时间:2016-02-28 20:57:36

标签: java jdbc sqlanywhere

我正在研究一个学校项目,我必须连接到一个在任何地方用SQL完成的数据库17,我一直在收到流动的错误:

  

找不到合适的jdbc驱动程序:sqlanywhere:Tds:l​​ocalhost:2638?eng =“DBNAME”。

欢迎任何帮助。

以下是进行连接的类的代码。

package model;

import java.sql.*;

public class DBConnection {

    public class ConnectionHandler {
        ResultSet r;
        Statement s;
        String returnStatus;
    }

    private String db, user, passwd;
    private String dbUrl;
    private Connection conn;

    public DBConnection(String db, String user, String passwd, Object openserver, String databasepath) {
        this.db = db;
        this.user = user;
        this.passwd = passwd;

        dbUrl = "jdbc:sqlanywhere:Tds:localhost:2638?eng=" + db;

        try {
            conn = DriverManager.getConnection(dbUrl, user, passwd);
            conn.setAutoCommit(false);
        } catch (Exception e) {
            System.out.println(e);
            Boolean openServer = (Boolean) openserver;
            if (openServer.booleanValue())
                try {
                    String engcommand = "dbeng12 " + databasepath + db + ".db";
                    Runtime.getRuntime().exec(engcommand);
                    long t0, t1;
                    t0 = System.currentTimeMillis();
                    do {
                        t1 = System.currentTimeMillis();
                    } while (t1 - t0 < 5000);
                    conn = DriverManager.getConnection(dbUrl, user, passwd);
                    conn.setAutoCommit(false);
                } catch (Exception ex) {
                    System.out.println("Unable to start server. " + ex);
                }
        }
    }

    public void updateStatement(String sql, ConnectionHandler ch) {
        Integer result = new Integer(0);
        String message = null;

        if (sql.trim().substring(0, 1).equalsIgnoreCase("U"))
            message = "Number of rows updated: ";
        else if (sql.trim().substring(0, 1).equalsIgnoreCase("I"))
            message = "Number of rows inserted: ";
        else
            message = " ";

        try {
            ch.s = conn.createStatement();
            result = new Integer(ch.s.executeUpdate(sql));
            ch.returnStatus = message + result;
        } catch (Exception e) {
            System.out.println("Unable to execute the insert/update/delete statement. " + e);
            ch.returnStatus = "Error:" + e.toString();
        }
    }

    public void returnResultSetSelectStatement(String sql, int resultsetType, ConnectionHandler ch) {
        ch.r = null;
        try {
            if (resultsetType == 1)
                ch.s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
            else
                ch.s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ch.r = ch.s.executeQuery(sql);

        } catch (Exception e) {
            ch.returnStatus = e.toString();
            System.out.println("Unable to execute the returnSelectStatement. " + e);
        }
    }

}

2 个答案:

答案 0 :(得分:1)

您需要将-s添加到程序的类路径中。如果您使用的是NetBeans或Eclipse等IDE,则可以将sajdbc4.jar添加为依赖库,NetBeans / Eclipse会自动将其添加到类路径中。

如果从命令行运行上述程序,请将sajdbc4.jar复制到上述Java程序所在的文件夹,然后使用以下命令编译该文件(这会将sajdbc4.jar添加到classpath) ,

sajdbc4.jar

使用以下命令运行Java程序(sajdbc4.jar已添加到类路径中),

javac -classpath ./sajdbc4.jar DbConnection.java

或者,如果您使用Maven构建项目,也可以添加Maven依赖项。

答案 1 :(得分:0)

使用"jdbc:sqlanywhere:DSN=SQL Anywhere 17 Demo;Password=sql" http://dcx.sap.com/sqla170/en/html/3bd5b17c6c5f1014945fd2b7ec7cba6a.html