Example usage for java.sql DriverManager getConnection

List of usage examples for java.sql DriverManager getConnection

Introduction

In this page you can find the example usage for java.sql DriverManager getConnection.

Prototype

@CallerSensitive
public static Connection getConnection(String url) throws SQLException 

Source Link

Document

Attempts to establish a connection to the given database URL.

Usage

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Procedure to avoid an explicit insert yourself.
 *//*from  w  ww .j  av  a2s. co m*/
public static void addRepo(String repoUrl) throws SQLException {
    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String query = "INSERT INTO localdb.sys_network.repositories (repo_url) " + "VALUES (?)";
    PreparedStatement ps = conn.prepareStatement(query);
    ps.setString(1, repoUrl);
    ps.execute();
    ps.close();
}

From source file:JDBCServlet.java

public void doGet(HttpServletRequest inRequest, HttpServletResponse outResponse)
        throws ServletException, IOException {

    PrintWriter out = null;/* ww  w.j  av  a2 s  .c o m*/
    Connection connection = null;
    Statement statement;
    ResultSet rs;

    try {
        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection("jdbc:mysql://localhost/products");
        statement = connection.createStatement();

        outResponse.setContentType("test/html");
        out = outResponse.getWriter();

        rs = statement.executeQuery("SELECT ID, title, price FROM product");

        out.println("<HTML><HEAD><TITLE>Products</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<UL>");

        while (rs.next()) {
            out.println(
                    "<LI>" + rs.getString("ID") + " " + rs.getString("title") + " " + rs.getString("price"));
        }

        out.println("</UL>");
        out.println("</BODY></HTML>");
    } catch (ClassNotFoundException e) {
        out.println("Driver Error");
    } catch (SQLException e) {
        out.println("SQLException: " + e.getMessage());
    }
}

From source file:com.googlecode.fascinator.DBIntegrationTestSuite.java

@AfterClass
public static void tearDown() throws IOException, BeansException, SQLException {
    Properties prop = new Properties();
    prop.load(new FileInputStream("src/test/resources/database.properties"));
    String dbString = prop.getProperty("jdbc.databaseurl");
    try {//from   w  w w  . j a  va  2 s.  c  o m
        DriverManager.getConnection(dbString + ";shutdown=true");
    } catch (SQLNonTransientConnectionException exception) {
    } catch (SQLException e) {
    }

    FileUtils.deleteDirectory(new File("target/database"));
}

From source file:org.works.integration.storedproc.derby.DerbyStoredProcedures.java

public static void findAllCoffeeBeverages(ResultSet[] coffeeBeverages) throws SQLException {

    Connection connection = null;
    PreparedStatement statement = null;

    connection = DriverManager.getConnection("jdbc:default:connection");
    String sql = "SELECT * FROM COFFEE_BEVERAGES";
    statement = connection.prepareStatement(sql);
    coffeeBeverages[0] = statement.executeQuery();

}

From source file:db_classes.DBManager.java

public static void shutdown() {
    try {/*from  ww w  .  j  ava2s .  co m*/
        DriverManager.getConnection("jdbc:postgresql:;shutdown=true");
    } catch (SQLException ex) {
        Logger.getLogger(DBManager.class.getName()).info(ex.getMessage());
    }
}

From source file:de.gmorling.scriptabledataset.ScriptableDataSetTest.java

@BeforeClass
public static void initializeConnection() throws Exception {

    connection = DriverManager.getConnection("jdbc:derby:derbyTest;create=true");
    connection.setAutoCommit(false);/*w ww.  j ava2s. com*/

    dbUnitConnection = new DatabaseConnection(connection);
}

From source file:com.example.mydtapp.JdbcInputAppTest.java

@BeforeClass
public static void setup() {
    try {//  www .  j  a  v  a  2 s  .  co m
        cleanup();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    try {
        Class.forName(DB_DRIVER).newInstance();

        Connection con = DriverManager.getConnection(URL);
        Statement stmt = con.createStatement();

        String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
                + " (ACCOUNT_NO INTEGER, NAME VARCHAR(255),AMOUNT INTEGER)";
        stmt.executeUpdate(createTable);
        cleanTable();
        insertEventsInTable(10, 0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}