List of usage examples for java.sql DriverManager getConnection
private static Connection getConnection(String url, java.util.Properties info, Class<?> caller) throws SQLException
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:XE"; String userName = "suru"; String password = "password"; Connection con = DriverManager.getConnection(url, userName, password); System.out.println("Connection success!"); Statement stmt = con.createStatement(); String sql = "CREATE TABLE EMP ( ID NUMBER(5) PRIMARY KEY, NAME VARCHAR2(50))"; stmt.execute(sql);/*from ww w . j a va2s . c o m*/ System.out.println("Table created successfully!"); stmt.close(); con.close(); }
From source file:ConnectionExample.java
public static void main(String args[]) { try {//from w ww. j av a2 s . co m Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.out.println("JDBC-ODBC driver failed to load."); return; } try { Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost/sampledb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password); String sql = "DELETE FROM users WHERE user_id = ?"; int userId = 2; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, userId);/*www .jav a 2 s.c o m*/ int rows = statement.executeUpdate(); System.out.println(rows + " record(s) deleted."); connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection dbConnection = null; String myConnectionString = ""; myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb"; dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever"); PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest"); ResultSet rs = stmt.executeQuery(); int i = 0;//from w ww. j av a 2 s . c om int j = 0; String s = ""; while (rs.next()) { i++; j = rs.getInt("id"); s = rs.getString("textcol"); } System.out.println(String.format("Finished reading %d rows.", i)); rs.close(); stmt.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection connection = null; Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/testdb"; String user = "root"; String password = ""; connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(); String sql = "INSERT INTO users (name, address) VALUES ('Foo', 'Street')"; stmt.execute(sql);/*from ww w.java2 s. com*/ connection.close(); }
From source file:Main.java
public static void main(String[] args) throws SQLException { // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@yourDB", "scott", "tiger"); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.setFetchSize(1);// w ww .j a v a 2s. c o m ResultSet rset = stmt.executeQuery("select EMPNO, ENAME, SAL from EMP"); showProperty(rset); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } doSomeChanges(conn); // Place the cursor before the first row rset.beforeFirst(); while (rset.next()) { System.out.println(rset.getInt(1) + " " + rset.getString(2) + " " + rset.getInt(3)); } rset.close(); stmt.close(); cleanup(conn); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "root"; Class.forName(driver);/* ww w .j a va 2 s.co m*/ Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM employee6"); ResultSetMetaData md = rs.getMetaData(); int col = md.getColumnCount(); System.out.println("Number of Column : " + col); for (int i = 1; i <= col; i++) { String col_name = md.getColumnName(i); System.out.println(col_name); } }
From source file:ExecuteExample.java
public static void main(String args[]) { try {//www . jav a2 s. c om Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { System.out.println("JDBC-ODBC driver failed to load."); return; } try { Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE SalesHistory(ProductID NUMBER,Price CURRENCY, TrnsDate DATE)"); stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection(url, username, password); CallableStatement stmt = conn.prepareCall("{call CREATE_USERS (?, ?, ?, ?, ?, ?)}"); stmt.setString(1, "a"); stmt.setString(2, "b"); stmt.setString(3, "c"); stmt.setString(4, "d"); stmt.setString(5, "e"); stmt.setString(6, "f"); stmt.execute();//from ww w.ja va 2 s .c o m conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott", "tiger"); conn.setAutoCommit(false);/*from w ww .ja v a 2 s . c o m*/ PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)"); ps.setString(1, file.getName()); ps.setBinaryStream(2, fis, (int) file.length()); ps.executeUpdate(); ps.close(); fis.close(); }