Open jimin-kiim opened 1 year ago
public static void JDBCexample(String dbid, String userid, String passwd) { try (Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd); // 1. Open a connection Statement stmt = conn.createStatement(); ) // 2. Create a “statement” object { … Do Actual Work …. } catch (SQLException sqle) { System.out.println("SQLException : " + sqle); } }
try { stmt.executeUpdate( "insert into instructor values('77987', 'Kim', 'Physics', 98000)"); } catch (SQLException sqle) { System.out.println("Could not insert tuple. " + sqle); }
ResultSet rset = stmt.executeQuery( "select dept_name, avg (salary) from instructor group by dept_name"); while (rset.next()) { System.out.println(rset.getString("dept_name") + " " + rset.getFloat(2)); }
PreparedStatement pStmt = conn.prepareStatement( "insert into instructor values(?,?,?,?)"); pStmt.setString(1, "88877"); pStmt.setString(2, "Perry"); pStmt.setString(3, "Finance"); pStmt.setInt(4, 125000); pStmt.executeUpdate(); pStmt.setString(1, "88878"); pStmt.executeUpdate();
JDBC
executeUpdate
executeQuery
prepareStatement