Java Database Connectivity: Difference between revisions

Content deleted Content added
No edit summary
mNo edit summary
Line 139:
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" ) ) {
/* you use the connection here */
} // the VM will take care of closing the connection
Line 148:
<source lang=java5>
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
}
</source>
Line 163:
ResultSet rs = stmt.executeQuery( "SELECT * FROM MyTable" )
) {
while ( rs.next() ) {
int numColumns = rs.getMetaData().getColumnCount();
for ( int i = 1 ; i <= numColumns ; i++ ) {
// Column numbers start at 1.
// Also there are many methods on the result set to return
// the column as a particular type. Refer to the Sun documentation
// for the list of valid conversions.
System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
}
}
Line 180:
<source lang=java5>
try (PreparedStatement ps =
conn.prepareStatement( "SELECT i.*, j.* FROM Omega i, Zappa j WHERE i.name = ? AND j.num = ?" )
) {
// In the SQL statement being prepared, each question mark is a placeholder
// that must be replaced with a value you provide through a "set" method invocation.
Line 194:
// positioned before the first row.
try (ResultSet rs = ps.executeQuery()) {
while ( rs.next() ) {
int numColumns = rs.getMetaData().getColumnCount();
for ( int i = 1 ; i <= numColumns ; i++ ) {
// Column numbers start at 1.
// Also there are many methods on the result set to return
// the column as a particular type. Refer to the Sun documentation
// for the list of valid conversions.
System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
} // for
} // while
Line 241:
Class.forName("com.mysql.jdbc.Driver");
 
Connection conn = DriverManager.getConnection(URL, "root"root”, "root");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO emp1 VALUES ('pctb5361'pctb5361’, 'gajanan'gajanan’, 'krpuram'krpuram’, 968666668)";
stmt.executeUpdate(sql);