本文整理汇总了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());
}
}
示例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();
}
}
示例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();
}
示例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;
}
}
}
示例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.");
}
示例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;
}
示例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();
}
}
示例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);
}
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
示例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;
}