简体   繁体   中英

Transact SQL Statements from java to Ms SQL Server 2014

Good morning developers (Its morning in Kenya). I have been working with Java and MySql databases. my newest client want me to create a solution based on his current installed database, running Microsoft Sql Server 2014 Enterprise Edition. The code below connects to the SQL Server instance but cannot transact any SQL Statements. i get the SQL Exception "Invalid Object Name". Please troubleshoot the code for me. thank you.

private java.sql.`enter code here`Connection con = null;
private final String url = "jdbc:sqlserver://";
private final String serverName = "localhost\\PHILIP";
private final String portNumber = "1433";
private final String databaseName = "Payrol";
private final String userName = "sa";
private final String password = "kukaphilip";

public Main() {

    System.setProperty("java.net.preferIPv6Addresses", "true");
    getConnection();
}

private String getConnectionUrl() {
    //System.out.println(java.lang.System.getProperty("java.library.path"));
    return "jdbc:jtds:sqlserver://localhost;Database=Payrol;integratedSecurity=true";
}

private java.sql.Connection getConnection() {
    try {
        /*Class.forName("net.sourceforge.jtds.jdbc.Driver");*/
                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection(getConnectionUrl());
        if (con != null) {
            System.out.println("Connection Successful!");
            String sql = "INSERT INTO payrol.dbo.test values('1','Philip')";
            Statement st = con.createStatement();
            st.executeUpdate(sql);
            /*st.setInt(1, 1);
            st.setString(2, "Philip");
            st.execute();*/
        }else{
            System.out.println("Connection failed");
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
    return con;
}

Check These

1)Try executing same query in SSMS to see if you get any error.

2)Put payrol.dbo.test into square brackets like [payrol].[dbo].[test]

3) If collation is case sensitive the cases of 'test' must be identical in database and in your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM