List of usage examples for java.lang Class forName
@CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException
From source file:RSMetaData.java
public static void main(String[] args) throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; Class.forName(driver).newInstance(); String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL"; Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM Employees"); printColumnInfo(rs);/*from w w w . j av a 2 s .c om*/ printColumnNames(rs); processRs(rs); rs = stmt.executeQuery("SELECT * FROM Location"); printColumnInfo(rs); printColumnNames(rs); processRs(rs); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); String[] functions = metadata.getStringFunctions().split(",\\s*"); for (int i = 0; i < functions.length; i++) { String function = functions[i]; System.out.println("Function = " + function); }/*from w w w. jav a 2 s. com*/ connection.close(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd"); Statement stmt = conn.createStatement(); String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)"; try {/*from w w w . java 2 s .co m*/ stmt.executeUpdate("DROP TABLE XML_Data"); } catch (SQLException se) { if (se.getErrorCode() == 942) System.out.println("Error dropping XML_Data table:" + se.getMessage()); } stmt.executeUpdate(streamingDataSql); File f = new File("employee.xml"); long fileLength = f.length(); FileInputStream fis = new FileInputStream(f); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)"); pstmt.setInt(1, 100); pstmt.setAsciiStream(2, fis, (int) fileLength); pstmt.execute(); fis.close(); ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100"); if (rset.next()) { InputStream xmlInputStream = rset.getAsciiStream(1); int c; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((c = xmlInputStream.read()) != -1) bos.write(c); System.out.println(bos.toString()); } conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); String[] functions = metadata.getNumericFunctions().split(",\\s*"); for (int i = 0; i < functions.length; i++) { String function = functions[i]; System.out.println("Function = " + function); }/* w w w .j a v a 2 s. c o m*/ connection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; Connection con;/*from www. j a v a 2 s . c o m*/ Statement stmt; ResultSet rs; try { Class.forName(driver); con = DriverManager.getConnection("jdbc:odbc:databaseName", "student", "student"); // Start a transaction con.setAutoCommit(false); stmt = con.createStatement(); stmt.addBatch("UPDATE EMP SET JOB = 1"); // Submit the batch of commands for this statement to the database stmt.executeBatch(); // Commit the transaction con.commit(); // Close the existing to be safe before opening a new one stmt.close(); // Print out the Employees stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM EMP"); // Loop through and print the employee number, job, and hiredate while (rs.next()) { int id = rs.getInt("EMPNO"); int job = rs.getInt("JOB"); String hireDate = rs.getString("HIREDATE"); System.out.println(id + ":" + job + ":" + hireDate); } con.close(); } catch (SQLException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* w w w .java2s . c o m*/ String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?"; PreparedStatement updateLastName = connection.prepareStatement(changeLastName); updateLastName.setString(1, "Martin"); // Set lastname placeholder value updateLastName.setInt(2, 4); // Set author ID placeholder value int rowsUpdated = updateLastName.executeUpdate(); // execute the update System.out.println("Rows affected: " + rowsUpdated); connection.close(); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } catch (SQLException sqle) { System.err.println(sqle); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); DatabaseMetaData metadata = connection.getMetaData(); String driverName = metadata.getDriverName(); String driverVersion = metadata.getDriverVersion(); int majorVersion = metadata.getDriverMajorVersion(); int minorVersion = metadata.getDriverMinorVersion(); System.out.println("driverName = " + driverName); System.out.println("driverVersion = " + driverVersion); System.out.println("majorVersion = " + majorVersion); System.out.println("minorVersion = " + minorVersion); connection.close();//from w ww .j a va2 s .com }
From source file:Main.java
public static void main(String[] args) throws Exception { try {/* www . j a v a 2 s . c om*/ String url = "jdbc:odbc:yourdatabasename"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT * from DUMMY"); System.out.println(rs.getType()); System.out.println(rs.getConcurrency()); rs.deleteRow(); rs.close(); rs.close(); stmt.close(); connection.close(); } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:odbc:databaseName"; String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String user = "guest"; String password = "guest"; try {/*w ww. j a v a2s. c om*/ Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); // Get the MetaData DatabaseMetaData metaData = conn.getMetaData(); // Get driver information System.out.println("Driver Informaion"); System.out.println(metaData.getDriverName()); System.out.println(metaData.getDriverVersion()); // Get schema information System.out.println("Schemas"); ResultSet schemas = metaData.getSchemas(); while (schemas.next()) { System.out.println(schemas.getString(1)); } // Get table information System.out.println("Tables"); ResultSet tables = metaData.getTables("", "", "", null); while (tables.next()) { System.out.println(tables.getString(3)); } conn.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://localhost/testdb"; String username = "root"; String password = ""; Class.forName("com.mysql.jdbc.Driver"); Connection conn = null;// w w w .j av a 2s.co m try { conn = DriverManager.getConnection(url, username, password); conn.setAutoCommit(false); Statement st = conn.createStatement(); st.execute("INSERT INTO orders (username, order_date) VALUES ('java', '2007-12-13')", Statement.RETURN_GENERATED_KEYS); ResultSet keys = st.getGeneratedKeys(); int id = 1; while (keys.next()) { id = keys.getInt(1); } PreparedStatement pst = conn.prepareStatement( "INSERT INTO order_details (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)"); pst.setInt(1, id); pst.setString(2, "1"); pst.setInt(3, 10); pst.setDouble(4, 100); pst.execute(); conn.commit(); System.out.println("Transaction commit..."); } catch (SQLException e) { if (conn != null) { conn.rollback(); System.out.println("Connection rollback..."); } e.printStackTrace(); } finally { if (conn != null && !conn.isClosed()) { conn.close(); } } }