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("com.mysql.jdbc.Driver").newInstance(); Connection conn = null;/*from ww w. j a v a 2s . c o m*/ conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); byte[] bkey = "This is some binary stuff".getBytes(); String query = "INSERT INTO keytable (name, `key`) VALUES (?,?)"; PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setString(1, "test"); pstmt.setBytes(2, bkey); pstmt.execute(); conn.close(); }
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_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); resultSet.afterLast();/*from ww w.jav a 2 s . co m*/ while (resultSet.previous()) { String productCode = resultSet.getString("product_code"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("quantity"); double price = resultSet.getDouble("price"); System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price); } connection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { int sum = 0;// w w w.ja v a2 s . c o m Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable"); while (res.next()) { int c = res.getInt(1); sum = sum + c; } System.out.println("Sum of column = " + sum); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "SELECT * FROM product WHERE year_made = ?"; PreparedStatement prest = con.prepareStatement(sql); prest.setInt(1, 2002);// w w w .ja v a 2s . c o m ResultSet rs1 = prest.executeQuery(); while (rs1.next()) { String mov_name = rs1.getString(1); int mad_year = rs1.getInt(2); System.out.println(mov_name + "\t- " + mad_year); } prest.setInt(1, 2003); ResultSet rs2 = prest.executeQuery(); while (rs2.next()) { String mov_name = rs2.getString(1); int mad_year = rs2.getInt(2); System.out.println(mov_name + "\t- " + mad_year); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String myDB = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/data.MDB"; Connection conn = DriverManager.getConnection(myDB, "", ""); }
From source file:Main.java
public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/testDb", "name", "pass"); Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String insertEmp1 = "insert into emp values(10,'A','trainee')"; String insertEmp2 = "insert into emp values(11,'B','trainee')"; String insertEmp3 = "insert into emp values(12,'C','trainee')"; con.setAutoCommit(false);//from www.j av a 2 s . com stmt.addBatch(insertEmp1); stmt.addBatch(insertEmp2); stmt.addBatch(insertEmp3); ResultSet rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows before batch execution= " + rs.getRow()); stmt.executeBatch(); con.commit(); System.out.println("Batch executed"); rs = stmt.executeQuery("select * from emp"); rs.last(); System.out.println("rows after batch execution= " + rs.getRow()); }
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_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); // Move to the second row resultSet.absolute(2);// ww w .j a v a2 s. c om System.out.println("You are now in: " + resultSet.getRow()); // Move 2 records forward from the current position (fourth row) resultSet.relative(2); System.out.println("You are now in: " + resultSet.getRow()); // Move to the last row in the result set resultSet.absolute(-1); System.out.println("You are now in: " + resultSet.getRow()); // Move 3 records backward from the current position (second row) resultSet.relative(-3); System.out.println("You are now in: " + resultSet.getRow()); connection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Date date = new Date(0); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); PreparedStatement prest = con.prepareStatement("INSERT Records VALUES(?,?,?)"); prest.setInt(1, 1);//from ww w .j a va 2s .c om prest.setString(2, "R"); prest.setDate(3, date.valueOf("1998-1-17")); int row = prest.executeUpdate(); }
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_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM products"); while (resultSet.next()) { String productCode = resultSet.getString("product_code"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("quantity"); double price = resultSet.getDouble("price"); System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price); }/*from www . java 2 s . co m*/ while (resultSet.previous()) { String productCode = resultSet.getString("product_code"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("quantity"); double price = resultSet.getDouble("price"); System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price); } connection.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Time time = new Time(0); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root"); String sql = "INSERT child VALUES(?,?)"; PreparedStatement prest = con.prepareStatement(sql); prest.setString(1, "vinod"); prest.setTime(2, time.valueOf("1:60:60")); int row = prest.executeUpdate(); System.out.println(row + " row(s) affectec)"); }