List of usage examples for java.lang Class forName
@CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException
From source file:InsertPictureToMySql.java
public static void main(String[] args) throws Exception, IOException, SQLException { Class.forName("org.gjt.mm.mysql.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root"); String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)"; FileInputStream fis = null;/*from w ww . j a v a 2s .c o m*/ PreparedStatement ps = null; try { conn.setAutoCommit(false); File file = new File("myPhoto.png"); fis = new FileInputStream(file); ps = conn.prepareStatement(INSERT_PICTURE); ps.setString(1, "001"); ps.setString(2, "name"); ps.setBinaryStream(3, fis, (int) file.length()); ps.executeUpdate(); conn.commit(); } finally { ps.close(); fis.close(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { try {//from www . j a va 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); String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP"; ResultSet rs = stmt.executeQuery(sqlQuery); rs.last(); // Move the cursor backwards through the ResultSet while (rs.previous()) { String nbr = rs.getString(1); String name = rs.getString(2); String job = rs.getString(3); String mgr = rs.getString(4); Timestamp hireDate = rs.getTimestamp(5); System.out.println(name); } 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 { Class.forName(DRIVER); Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); connection.close();/*ww w.j ava 2 s . c o m*/ }
From source file:TableMaker.java
public static void main(String[] args) throws Exception { Class.forName(jdbcDriver); url += dbName;/*from w w w . j av a 2 s. c om*/ Connection con = null; Statement stmt = null; con = DriverManager.getConnection(url); stmt = con.createStatement(); stmt.execute(SQLCreate); con.close(); if (con != null) { con.close(); } if (stmt != null) { stmt.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName(DB_DRIVER); Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD); CallableStatement callableStatement = null; String getPERSONByUserIdSql = "{call getPERSONByUserId(?,?,?,?)}"; callableStatement = dbConnection.prepareCall(getPERSONByUserIdSql); callableStatement.setInt(1, 10);/* ww w .jav a 2 s . c o m*/ callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR); callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR); callableStatement.registerOutParameter(4, java.sql.Types.DATE); callableStatement.executeUpdate(); String userName = callableStatement.getString(2); String createdBy = callableStatement.getString(3); Date createdDate = callableStatement.getDate(4); System.out.println("UserName : " + userName); System.out.println("CreatedBy : " + createdBy); System.out.println("CreatedDate : " + createdDate); callableStatement.close(); dbConnection.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String url = "jdbc:mysql://192.168.100.100:3306/"; String dbName = "databaseName"; Statement stmt = null;//from w w w. ja va2 s. com ResultSet result = null; String driver = "com.mysql.jdbc.Driver"; String databaseUserName = "admin"; String databasePassword = "root"; Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword); stmt = conn.createStatement(); result = null; String password, username; result = stmt.executeQuery("select * from userTable where username ='user1' "); if (!result.isBeforeFirst()) { System.out.println("resultset contin no rows"); } while (result.next()) { username = result.getString("username"); password = result.getString("password"); System.out.println(username + " " + password); } conn.close(); }
From source file:GetSerVersUID.java
public static void main(String[] av) throws Exception { // First we construct a Class object for the given class Class cl = Class.forName("Candidate"); // Then an ObjectStreamClass for the given class ObjectStreamClass ocl = ObjectStreamClass.lookup(cl); // And use the ObjectStreamClass to get the Class' // unique SerialVersionUID System.out.println("For class " + cl); System.out.println("static final long serialVersionUID = " + ocl.getSerialVersionUID() + "L;"); // must be long // And just for reference... System.out.println("(Must range from " + Long.MIN_VALUE + " to " + Long.MAX_VALUE + ".)"); }
From source file:Main.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(); createBlobClobTables(stmt);/*www. ja v a 2s . c om*/ PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)"); File file = new File("blob.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); file = new File("clob.txt"); fis = new FileInputStream(file); pstmt.setAsciiStream(2, fis, (int) file.length()); fis.close(); pstmt.execute(); ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40"); rs.next(); java.sql.Blob blob = rs.getBlob(2); java.sql.Clob clob = rs.getClob(3); byte blobVal[] = new byte[(int) blob.length()]; InputStream blobIs = blob.getBinaryStream(); blobIs.read(blobVal); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(blobVal); blobIs.close(); char clobVal[] = new char[(int) clob.length()]; Reader r = clob.getCharacterStream(); r.read(clobVal); StringWriter sw = new StringWriter(); sw.write(clobVal); r.close(); conn.close(); }
From source file:Main.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(); createBlobClobTables(stmt);/*www . j a v a2 s .c o m*/ PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobClob VALUES(40,?,?)"); File file = new File("blob.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); file = new File("clob.txt"); fis = new FileInputStream(file); pstmt.setAsciiStream(2, fis, (int) file.length()); fis.close(); pstmt.execute(); ResultSet rs = stmt.executeQuery("SELECT * FROM BlobClob WHERE id = 40"); rs.next(); java.sql.Blob blob = rs.getBlob(2); java.sql.Clob clob = rs.getClob("myClobColumn"); byte blobVal[] = new byte[(int) blob.length()]; InputStream blobIs = blob.getBinaryStream(); blobIs.read(blobVal); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(blobVal); blobIs.close(); char clobVal[] = new char[(int) clob.length()]; Reader r = clob.getCharacterStream(); r.read(clobVal); StringWriter sw = new StringWriter(); sw.write(clobVal); r.close(); conn.close(); }
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"; String theStatement = "SELECT lastname, firstname FROM autors"; try {//from w w w. j a v a 2 s. co m Class.forName(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement queryAuthors = connection.createStatement(); ResultSet theResults = queryAuthors.executeQuery(theStatement); queryAuthors.close(); } catch (ClassNotFoundException cnfe) { System.err.println(cnfe); } catch (SQLException sqle) { String sqlMessage = sqle.getMessage(); String sqlState = sqle.getSQLState(); int vendorCode = sqle.getErrorCode(); System.err.println("Exception occurred:"); System.err.println("Message: " + sqlMessage); System.err.println("SQL state: " + sqlState); System.err.println("Vendor code: " + vendorCode + "\n----------------"); } }