当前位置: 首页>>代码示例>>Java>>正文


Java DriverManager.getConnection方法代码示例

本文整理汇总了Java中java.sql.DriverManager.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java DriverManager.getConnection方法的具体用法?Java DriverManager.getConnection怎么用?Java DriverManager.getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.sql.DriverManager的用法示例。


在下文中一共展示了DriverManager.getConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getConnection

import java.sql.DriverManager; //导入方法依赖的package包/类
private static Connection getConnection() throws SQLException {

        try {
            Class.forName(drv).newInstance();

            return DriverManager.getConnection(url, usr, pwd);
        } catch (SQLException se) {
            throw se;
        } catch (Exception e) {
            throw new SQLException(e.toString());
        }
    }
 
开发者ID:tiweGH,项目名称:OpenDiabetes,代码行数:13,代码来源:TriggerSample.java

示例2: addPartition

import java.sql.DriverManager; //导入方法依赖的package包/类
@Test
public void addPartition() throws Exception {
  String tableName = "my_table";
  createPartitionedTable(DATABASE, tableName);

  try (Connection connection = DriverManager.getConnection(server.connectionURL());
      Statement statement = connection.createStatement()) {
    String addPartitionHql = String.format("ALTER TABLE %s.%s ADD PARTITION (partcol=1)", DATABASE, tableName);
    statement.execute(addPartitionHql);
  }

  HiveMetaStoreClient client = server.newClient();
  try {
    List<Partition> partitions = client.listPartitions(DATABASE, tableName, (short) -1);
    assertThat(partitions.size(), is(1));
    assertThat(partitions.get(0).getDbName(), is(DATABASE));
    assertThat(partitions.get(0).getTableName(), is(tableName));
    assertThat(partitions.get(0).getValues(), is(Arrays.asList("1")));
    assertThat(partitions.get(0).getSd().getLocation(),
        is(String.format("file:%s/%s/%s/partcol=1", server.temporaryFolder.getRoot(), DATABASE, tableName)));
  } finally {
    client.close();
  }
}
 
开发者ID:HotelsDotCom,项目名称:beeju,代码行数:25,代码来源:HiveServer2JUnitRuleTest.java

示例3: removeDB

import java.sql.DriverManager; //导入方法依赖的package包/类
public void removeDB(String publicId){
    try {
        Class.forName("org.sqlite.JDBC") ;
        conn = DriverManager.getConnection("jdbc:sqlite:DataBase.db") ;
        conn.setAutoCommit(false) ;
        stmt = conn.createStatement() ;
        String sql = "DELETE from IMAGE where URLID='"+ publicId + "';";
        stmt.executeUpdate(sql) ;
        conn.commit() ;
        stmt.close() ;
        conn.close() ;

    } catch ( Exception e ) {
        System.err.println( e.getClass() .getName() + ": " + e.getMessage() );
        System.exit(0) ;
    }
    System.out.println("Operation done successfully") ;
    showDB();
}
 
开发者ID:maximova136,项目名称:j-demo,代码行数:20,代码来源:DbHandler.java

示例4: getMySQLConnection

import java.sql.DriverManager; //导入方法依赖的package包/类
/**
 * Gets the {@link Connection} to the database
 *
 * @return the {@link Connection}
 */
public Connection getMySQLConnection() {
    if (isConnected()) {
        return CONNECTIONS.get(database);
    } else {
        try {
            Connection c = DriverManager.getConnection(
                    "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", username, password);

            CONNECTIONS.put(database, c);

            return c;
        } catch (SQLException ignore) {
            Bukkit.getLogger().log(Level.WARNING, "Failed to reconnect to " + database + "! Check your sql.yml inside AlphaGameLibary");
            return null;
        }
    }
}
 
开发者ID:AlphaHelixDev,项目名称:AlphaLibary,代码行数:23,代码来源:MySQLAPI.java

示例5: setUp

import java.sql.DriverManager; //导入方法依赖的package包/类
@Before
public void setUp() {
  super.setUp();

  LOG.debug("Setting up postgresql test: " + CONNECT_STRING);

  try {
    connection = DriverManager.getConnection(CONNECT_STRING, DATABASE_USER, PASSWORD);
    connection.setAutoCommit(false);
  } catch (SQLException ex) {
    LOG.error("Can't create connection", ex);
    throw new RuntimeException(ex);
  }

  createTable(TABLE_NAME, SCHEMA_PUBLIC);
  createTable(STAGING_TABLE_NAME, SCHEMA_PUBLIC);
  createTable(TABLE_NAME, SCHEMA_SPECIAL);
  createTable(STAGING_TABLE_NAME, SCHEMA_SPECIAL);
  createProcedure(PROCEDURE_NAME, SCHEMA_PUBLIC);

  LOG.debug("setUp complete.");
}
 
开发者ID:aliyun,项目名称:aliyun-maxcompute-data-collectors,代码行数:23,代码来源:PostgresqlExportTest.java

示例6: conectarSpr

import java.sql.DriverManager; //导入方法依赖的package包/类
public static Connection conectarSpr(){
	 Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        //try {Class.forName("com.mysql.jdbc.Driver");}
        try {Class.forName("org.postgresql.Driver");}
        catch (ClassNotFoundException e) {e.printStackTrace();}
        String url = "";
        //String user = "root";
        String user = "postgres";
        String password = "postgres";

        //try {con = DriverManager.getConnection("jdbc:mysql://mysql01.stp.gov.py/spr?useUnicode=true&characterEncoding=UTF-8", "root", "postgres");}
        try {con = DriverManager.getConnection("jdbc:postgresql://pg01.stp.gov.py/spr?useUnicode=true&characterEncoding=UTF-8", "postgres", "postgres");}
        catch (SQLException ex) {
            Logger lgr = Logger.getLogger(SqlHelper.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        } 
        return con;
}
 
开发者ID:stppy,项目名称:tcp,代码行数:21,代码来源:ConnectionConfiguration.java

示例7: DBUtils

import java.sql.DriverManager; //导入方法依赖的package包/类
private DBUtils()
{
    try
    {
        ConfigFactory config = ConfigFactory.Instance();
        DRIVER = config.getProperty("driver");
        URL = config.getProperty("url");
        USERID = config.getProperty("user");
        USERPASSWORD = config.getProperty("password");

        Class.forName(DRIVER);
        conn = DriverManager.getConnection(URL, USERID, USERPASSWORD);
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}
 
开发者ID:dbiir,项目名称:rainbow,代码行数:18,代码来源:DBUtils.java

示例8: get

import java.sql.DriverManager; //导入方法依赖的package包/类
public Connection get() {
    try {
        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

        String url = "jdbc:mysql://" +       //db type
                "localhost:" +               //host name
                "3306/" +                    //port
                "db_example?" +              //db name
                "useSSL=false&" +            //do not use ssl
                "user=tully&" +              //login
                "password=tully";            //password

        return DriverManager.getConnection(url);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_04,代码行数:18,代码来源:JDBCConnectionFactory.java

示例9: getConn

import java.sql.DriverManager; //导入方法依赖的package包/类
public static Connection getConn(String alias) {
	Connection conn = null;
	try {
		conn = DriverManager.getConnection(alias);
	} catch (SQLException e) {
		e.printStackTrace();
		logger.error(e.getMessage(), e);
	}
	return conn;
}
 
开发者ID:kingston-csj,项目名称:jforgame,代码行数:11,代码来源:DbHelper.java

示例10: setUpConnection

import java.sql.DriverManager; //导入方法依赖的package包/类
@BeforeClass
public static void setUpConnection() throws Exception {
  // Get JDBC connection to Dremio:
  // (Note: Can't use JdbcTest's connect(...) because JdbcTest closes
  // Connection--and other JDBC objects--on test method failure, but this test
  // class uses connection (and dependent DatabaseMetaData object) across
  // methods.)
  Driver.load();
  connection = DriverManager.getConnection( "jdbc:dremio:zk=local" );
  dbMetadata = connection.getMetaData();
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:12,代码来源:Drill2128GetColumnsDataTypeNotTypeCodeIntBugsTest.java

示例11: getAdminConnectionWithProps

import java.sql.DriverManager; //导入方法依赖的package包/类
protected Connection getAdminConnectionWithProps(Properties props) throws SQLException {
    String adminUrl = System.getProperty(ADMIN_CONNECTION_PROPERTY_NAME);

    if (adminUrl != null) {
        return DriverManager.getConnection(adminUrl, props);
    }
    return null;
}
 
开发者ID:Jugendhackt,项目名称:OpenVertretung,代码行数:9,代码来源:BaseTestCase.java

示例12: getInstance

import java.sql.DriverManager; //导入方法依赖的package包/类
public static Connection getInstance() throws DAOConfigurationException{
    if (connection == null){
        try{
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e){
            throw new DAOConfigurationException(e);
        }
    }
    return connection;
}
 
开发者ID:antonin-arquey,项目名称:polyevent,代码行数:11,代码来源:ConnectionMySQL.java

示例13: getDataSourceConnection

import java.sql.DriverManager; //导入方法依赖的package包/类
private static Connection getDataSourceConnection() {
    try {
        Class.forName(DRIVER_CLASS_NAME);
        conn = DriverManager.getConnection(dbAddress, dbUserName, dbPassword);
    } catch (ClassNotFoundException | SQLException ex) {
        log.error("Exception occurred while get Connection : " + ex, ex);
    }
    return conn;
}
 
开发者ID:sundarcse1216,项目名称:rest-api,代码行数:10,代码来源:DataSourceConnection.java

示例14: MountieQueries

import java.sql.DriverManager; //导入方法依赖的package包/类
/**
 * Constructor with one argument; used mostly for SELECT * queries.
 * @param sql The SQL statement to be executed.
 */
public MountieQueries(String sql) {
    try{
        setSQL(sql);
        connection = DriverManager.getConnection(URL, userDB, passDB); 
        preparedStatment = connection.prepareStatement(sql);
    }
    catch (SQLException sqlException) {
     sqlException.printStackTrace();
     displayAlert(Alert.AlertType.ERROR,"Error" , 
            "Data base could not be loaded", searchString);
     System.exit(1);
  } 
}
 
开发者ID:pxndroid,项目名称:mountieLibrary,代码行数:18,代码来源:MountieQueries.java

示例15: databaseExists

import java.sql.DriverManager; //导入方法依赖的package包/类
static boolean databaseExists(String serverName,
        String dbName) {
    String connectionString = Configuration.getConnectionString(serverName, dbName);
    Connection conn = null;
    try {
        conn = DriverManager.getConnection(connectionString);
        String query = "select count(*) from sys.databases where name = '" + dbName + "';";
        try (Statement stmt = conn.createStatement()) {
            ResultSet rs = stmt.executeQuery(query);
            return rs.next();
        }
        catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    catch (Exception e) {
        ConsoleUtils.writeWarning("Failed to connect to SQL database with connection string:");
        System.out.printf("\n%1$s\n" + "\r\n", connectionString);
        ConsoleUtils.writeWarning("If this connection string is incorrect, please update the" + "Configuration file.\r\nException message: %s",
                e.getMessage());
        return false;
    }
    finally {
        connFinally(conn);
    }
    return true;
}
 
开发者ID:Microsoft,项目名称:elastic-db-tools-for-java,代码行数:28,代码来源:SqlDatabaseUtils.java


注:本文中的java.sql.DriverManager.getConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。