zoukankan      html  css  js  c++  java
  • JDBC连接SQL Server测试代码及异常 全新时代

    import java.sql.*;
    public class SqlServerTest {
    
        // 驱动类
    
    //    static String driverClass = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
        static String driverClass = "com.mysql.jdbc.Driver";
    
        // 连接字符串
    
    //    static String url = "jdbc:microsoft:sqlserver://HNHJ\\HNHJ2:1433;dataBaseName=db_net";
        static String url = "jdbc:mysql://127.0.0.1:3306/db_net?characterEncoding=utf8&autoReconnect=true";
        
        // 密码
    
    //    static String password = "sa";
        static String password = "test";
    
        // 用户名
    
    //    static String username = "";
        static String username = "test";
    
        // 待执行的 SQL 语句
    
        static String sql = "SELECT * FROM tb_news";
    
        public static void main(String[] args) {
    
            Connection conn = null;
    
            PreparedStatement pstmt = null;
    
            ResultSet rs = null;
    
            try {
    
                Class.forName(driverClass);
    
                conn = DriverManager.getConnection(url, username, password);
    
                pstmt = conn.prepareStatement(sql);
    
                rs = pstmt.executeQuery();
    
                while (rs.next()) {
    
                    System.out.println("OK.");
    
                }
    
                System.out.println("OK too.");
    
                rs.close();
    
                pstmt.close();
    
                conn.close();
    
            } catch (ClassNotFoundException e) {
    
                System.out.println(" 驱动类没有找到 .");
    
                e.printStackTrace();
    
            } catch (SQLException e) {
    
                e.printStackTrace();
    
            } finally {
    
                if (rs != null) // 结果集没有关闭时关闭结果集
    
                    try {
    
                        rs.close();
    
                    } catch (SQLException e) {
    
                        e.printStackTrace();
    
                    }
    
                if (pstmt != null) // 发送对象没有关闭时关闭发送对象
    
                    try {
    
                        pstmt.close();
    
                    } catch (SQLException e) {
    
                        e.printStackTrace();
    
                    }
    
                if (conn != null) // 连接没有关闭时关闭连接
    
                    try {
    
                        conn.close();
    
                    } catch (SQLException e) {
    
                        e.printStackTrace();
    
                    }
            }
    
        }
    
    }

    java.sql.SQLException : [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
    --查看端口号及测试连接--
    方法:Microsoft SQL Server ->SQL Server 组 ->选择数据库(如:HNHJ\HNHJ2)
    ->属性 ->常规 ->网络配置 ->TCP/IP属性 ->查看默认端口号。


    Cannot create JDBC driver of class '' for connect URL 'null'
    --更换数据源连接方式-- 避免Tomcat管理数据源。

  • 相关阅读:
    select、poll和epoll
    Linux 常用命令之文件和目录
    SmartPlant Review 帮助文档机翻做培训手册
    SmartPlant Foundation 基础教程 3.4 菜单栏
    SmartPlant Foundation 基础教程 3.3 标题栏
    SmartPlant Foundation 基础教程 3.2 界面布局
    SmartPlant Foundation 基础教程 3.1 DTC登陆界面
    SmartPlant Foundation 基础教程 1.4 SPF架构
    SmartPlant Foundation 基础教程 1.3 SPF其他功能
    SmartPlant Foundation 基础教程 1.2 SPF集成设计功能
  • 原文地址:https://www.cnblogs.com/simpledev/p/3003867.html
Copyright © 2011-2022 走看看