无法使用java 8连接到sql服务器

时间:2022-01-20 15:36:58

I'm trying to connect to my sql server 21014 using java 8. But i'm getting error.

我正在尝试使用java 8连接到我的sql server 21014。但我得到错误。

import java.sql.*;
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class DBConnec
{
    public static void main(String a[])
    {

        try
        {
            String url = "jdbc:jtds:sqlserver//localhost:1433/dictionary";   
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url);
            System.out.println("connection created");
            Statement st=conn.createStatement();    
            String sql="select * from data where word LIKE 'hi'";
            ResultSet rs=st.executeQuery(sql);

            if (rs.next()) 
            {
                System.out.println(rs.getString(0));
            }

            if(st!=null)
                st.close();
            if(conn!=null)
                conn.close();
        }
        catch(SQLException sqle)
        {
            sqle.printStackTrace();
        }

        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }

    }
}

Exception i'm getting is:

例外我是:

java.sql.SQLException: The syntax of the connection URL 'jdbc:jtds:sqlserver//localhost:1433/dictionary' is invalid. at net.sourceforge.jtds.jdbc.Driver.setupConnectProperties(Driver.java:241) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:181) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at DBConnec.main(DBConnec.java:15)

java.sql。SQLException:连接URL“jdbc:jtds:sqlserver/ localhost:1433/dictionary”的语法无效。在net.sourceforge.jtds.jdbc.Driver.setupConnectProperties(Driver.java:241),在net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:181),在java.sql.DriverManager。在java.sql.DriverManager getConnection(未知源)。getConnection(未知源)DBConnec.main(DBConnec.java:15)

I'm running my code using this command:

我使用以下命令运行代码:

java -cp .;"C:\Program Files\Java\jdk1.8.0_66\jre\lib\ext\jtds-1.3.0.jar" DBConnec

java - cp。“C:\ Program Files \ Java \ jdk1.8.0_66 \ jre \ lib \ ext \ jtds-1.3.0。jar”DBConnec

I've also tried url without writing "sqlserver". But it gives same exception. Please help.. Thank you..

我也尝试过不写“sqlserver”的url。但它也有同样的例外。请帮助. .谢谢你!

I've corrected my syntax but now i'm getting exception like this:

我已经纠正了我的语法,但现在我有这样的例外:

java.sql.SQLException: Network error IOException: Connection refused: connect at net.sourceforge.jtds.jdbc.JtdsConnection.(JtdsConnection.java:434) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:183) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at DBConnec.main(DBConnec.java:15) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) 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)

java.sql。SQLException:网络错误IOException:连接被拒绝:连接在net. sourceforge.jdbc.jtdsconnection .(JtdsConnection.java:434)在net. sourceforge.jdbc . driver .connect(Driver.java:183) at java.sql.DriverManager。在java.sql.DriverManager getConnection(未知源)。由:java.net.ConnectException:连接被拒绝:在java.net. dualstackplain .connect0(本机方法)上连接sun.reflect.NativeMethodAccessorImpl。在sun.reflect.NativeMethodAccessorImpl invoke0(本地方法)。在sun.reflect.DelegatingMethodAccessorImpl调用(未知源)。在java.lang.reflect.Method调用(未知源)。调用(未知源)

2 个解决方案

#1


1  

Try this:

试试这个:

String url = "jdbc:jtds:sqlserver://localhost:1433/dictionary";

You missed the colon after sqlserver.

您在sqlserver之后错过了冒号。

You should not be adding JDBC driver JARs to the jre/lib/ext directory. Learn how to use CLASSPATH properly.

不应该将JDBC驱动程序jar添加到jre/lib/ext目录。学习如何正确地使用类路径。

#2


1  

The url format for JTDS is:

JTDS的url格式为:

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

So your url should be like:

所以你的url应该是:

String url = "jdbc:jtds:sqlserver://localhost:1433/dictionary"; 
                                ^^^____missing colon

#1


1  

Try this:

试试这个:

String url = "jdbc:jtds:sqlserver://localhost:1433/dictionary";

You missed the colon after sqlserver.

您在sqlserver之后错过了冒号。

You should not be adding JDBC driver JARs to the jre/lib/ext directory. Learn how to use CLASSPATH properly.

不应该将JDBC驱动程序jar添加到jre/lib/ext目录。学习如何正确地使用类路径。

#2


1  

The url format for JTDS is:

JTDS的url格式为:

jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]

So your url should be like:

所以你的url应该是:

String url = "jdbc:jtds:sqlserver://localhost:1433/dictionary"; 
                                ^^^____missing colon