zoukankan      html  css  js  c++  java
  • 连接数据测试代码

    package com.foen.cloud.utils;


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


    public class DB_Conn{
    //JDBC驱动程序名
    private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    //数据库名称
    private static String dateBaseName = "##";
    //用户名
    private static String username = "###";
    //密码
    private static String password = "##";
    private static String url="jdbc:sqlserver://###;databaseName="+dateBaseName;
    private static Connection conn;
    private static Statement sm;

    public static void main(String[] args) throws Exception {
    connectDB();
    closeDB();

    }
    //建立连接
    public static void connectDB() {
    try {
    try {
    Class.forName(driverName).newInstance();
    } catch (InstantiationException e) {
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    conn = DriverManager.getConnection(url,username,password);
    sm = conn.createStatement();
    System.out.println("数据库连接成功!"+sm);
    } catch (SQLException e) {
    e.printStackTrace();
    System.out.println("数据库连接失败!");
    }
    }
    //释放连接
    public static void closeDB(){
    try{
    if(sm != null){
    sm.close();
    }
    }catch(SQLException e){
    System.out.println("关闭Statement失败!");
    }
    try{
    if(conn != null){
    conn.close();
    }
    }catch(SQLException e){
    System.out.println("关闭Connection失败!");
    }
    }
    public Statement getStatement() {
    return sm;
    }
    public void setStatement(Statement sm) {
    this.sm = sm;
    }
    }

  • 相关阅读:
    设计模式之观察者模式
    设计模式之模板方法模式
    设计模式之代理模式(全面讲解)
    设计模式之工厂模式(包含三种模式)
    设计模式之单例模式(包含三种方式)
    opencv+vs2015 堆内存析构异常
    用python来压缩文件
    GPT安装ubuntu的问题
    Two Sum and add Two Numbers
    [LeetCode] Palindrome Partitioning II
  • 原文地址:https://www.cnblogs.com/gzhbk/p/11912314.html
Copyright © 2011-2022 走看看