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[] argv) throws Exception { int records = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT COUNT(*) FROM mytable"; PreparedStatement prest = con.prepareStatement(sql); ResultSet rs = prest.executeQuery(); while (rs.next()) { records = rs.getInt(1);/*from w w w.j av a 2 s . c o m*/ } System.out.println("Number of records: " + records); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection m_Connection = DriverManager.getConnection( "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase", "userid", "password"); Statement m_Statement = m_Connection.createStatement(); String query = "SELECT * FROM MyTable"; ResultSet m_ResultSet = m_Statement.executeQuery(query); while (m_ResultSet.next()) { System.out.println(/* www.jav a2 s . c o m*/ m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", " + m_ResultSet.getString(3)); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID", "PASSWORD"); CallableStatement proc_stmt = con.prepareCall("{ call generateID(?) }"); proc_stmt.setString(1, "employee"); ResultSet rs = proc_stmt.executeQuery(); if (rs.next()) { int employeeId = rs.getInt(1); System.out.println("Generated employeeId: " + employeeId); } else {// www .j a v a 2s. c o m System.out.println("Stored procedure couldn't generate new Id"); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { int count = 0; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2000);// ww w.ja v a 2 s . c om prest.setInt(2, 2009); ResultSet rs = prest.executeQuery(); while (rs.next()) { String mov_name = rs.getString(1); int mov_year = rs.getInt(2); count++; System.out.println(mov_name + "\t" + "- " + mov_year); } System.out.println("Number of records: " + count); prest.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { String hostname = "", dbname = "", username = "", password = ""; Class.forName("org.postgresql.Driver"); Connection connection = DriverManager.getConnection("jdbc:postgresql://" + hostname + "/" + dbname, username, password);//from w w w .j ava 2 s .c o m ResultSet rs = connection.createStatement().executeQuery("select version() as version"); while (rs.next()) { System.out.println(rs.getString("version")); } }
From source file:ResultSetExample.java
public static void main(String args[]) { try {/*from w ww .j a v a 2 s . co m*/ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", ""); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT SupplierName,ProductName, Price " + "FROM ProductSuppliersView WHERE CategoryName LIKE '%BEVERAGES%' "); while (rs.next()) { String supplier = rs.getString("SupplierName"); String product = rs.getString("ProductName"); int price = rs.getInt("Price"); System.out.println(supplier + " sells " + product + " for $" + price); } stmt.close(); con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
public static void main(String args[]) throws Exception { String connectionURL = "jdbc:mysql://localhost:3306/test"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(connectionURL, "login", "password"); Statement stmt = con.createStatement(); stmt.execute("CREATE TRIGGER obs_update BEFORE UPDATE ON obs " // + "FOR EACH ROW "// + "BEGIN "// + "IF OLD.voided = 0 AND NEW.voided = 1 THEN "// + " DELETE FROM emp WHERE id = OLD.obs_id; "// + "ELSE "// + " UPDATE emp SET emp.revision_token = NOW() "// + " WHERE NEW.id = emp.id; "// + "END IF; "// + "END;"); con.close();/* w w w . j av a 2 s .com*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); System.out.println("MySQL JDBC Driver Registered!"); Connection connection = null; connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "password"); if (connection != null) { System.out.println("database now!"); } else {/*from w w w. j a va 2 s .c om*/ System.out.println("Failed to make connection!"); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", ""); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String query = "SELECT id, code, name, quantity, price FROM products"; ResultSet uprs = statement.executeQuery(query); while (uprs.next()) { System.out.println(uprs.getString("id") + ":" + uprs.getString("code") + ":" + uprs.getString("name") + ":" + uprs.getInt("quantity") + ":" + uprs.getDouble("price")); }// w ww . j a va 2s . c o m uprs.first(); uprs.updateString("name", "Java"); uprs.updateRow(); uprs.next(); uprs.deleteRow(); uprs.moveToInsertRow(); uprs.updateString("code", "1"); uprs.updateString("name", "Data Structures"); uprs.updateInt("quantity", 1); uprs.updateDouble("price", 5.99); uprs.insertRow(); uprs.beforeFirst(); while (uprs.next()) { System.out.println(uprs.getString("id") + "\t" + uprs.getString("code") + "\t" + uprs.getString("name") + "\t" + uprs.getInt("quantity") + "\t" + uprs.getDouble("price")); } connection.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Connection con = null;// w w w . j av a2 s.co m Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@192.201.32.92:1521:psprd1", "username", "password"); String query = null; ResultSet rset = null; query = "UPDATE t1 " + " SET id = ?"; PreparedStatement stmt = con.prepareStatement(query); // stmt.setInt(paramIndex++, null); stmt.setNull(1, java.sql.Types.INTEGER); stmt.executeUpdate(); stmt.close(); query = "select id from t1 "; stmt = con.prepareStatement(query); rset = stmt.executeQuery(); rset.next(); System.out.println(rset.getString("id")); rset.close(); stmt.close(); con.close(); }