JDBC连接url问题

时间:2017-11-17 09:35:09

标签: java sql-server jdbc

我在下面的url连接上收到错误“令牌上的语法错误,删除这些令牌”
我的网址

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class JdbcSQLServerConnection {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {

    String url="jdbc:sqlserver://"TKSQA";databaseName="RDK";integratedSecurity=true";


    String query="use RDK 
    go
    /* * the current date must be less than 45 days after the policy effective date and the transaction must have
           * a status of IssuedRenewal */
    select top 5 left(prsnAddr.PostalZip, 5) as ZipCode, policy.policynum,terms.termeffective
    from PolicyRecord pr left join Policy policy on pr.PolicySK = policy.PolicySK
    join PolicyTerms terms on pr.PolicyTermSK = terms.PolicyTermSK and getDate() < (terms.termeffective + 45)
    join PolicyTxn txn on pr.PolicyTxnSK = txn.PolicyTxnSK and TxnBusStatus = 'ISSUEDRENEWAL'  and txn.TxnSequence = (
           select max(ptx2.TxnSequence) 
           from PolicyTxn ptx2 
           inner join PolicyTermTxn pttx2 on pttx2.PolicyTxnsk = ptx2.PolicyTxnSK
           where pttx2.PolicyTermSK = terms.PolicyTermSK and ptx2.TxnStatus <> 'INACTIVE') 
    join Insured ins on ins.PolicyRecordSK= pr.PolicyRecordSK
    join Person pers on pers.PersonSK = ins.PersonSK
    join PersonAddress prsnAddr on prsnAddr.PersonSK = pers.PersonSK
           where policy.insuranceproductid like '%CollectorVehicle%' and RecordType='ONSET' 
           order by terms.TermEffective
    ";


    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection con = DriverManager.getConnection(url);
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);

    rs.next();
    String zip= rs.getString("ZipCode");
    System.out.print(zip);
    st.close();
    con.close();


        }

    }

由于上述错误,我无法使用java连接sql服务器。这是写字符串网址的正确方法

1 个答案:

答案 0 :(得分:-1)

连接URL的一般形式是(对于MS SQL Server)

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

例如,

jdbc:sqlserver://localhost;user=MyUserName;password=*****;

连接URL的一般形式是(对于MySQL服务器)

jdbc:mysql://hostname/ databaseName

此处,hostname应包含portnumber(必需)

例如,

  //STEP 1. Import required packages
import java.sql.*;

public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";

   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to a selected database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      System.out.println("Connected database successfully...");

      //STEP 4: Execute a query

   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }finally{
    conn.close();
   }//end finally
   System.out.println("Goodbye!");
}
}

<强>参考,

https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url

https://www.tutorialspoint.com/jdbc/index.htm