UPDATE
À̹ø °Á¿¡¼´Â JDBC¸¦ ÀÌ¿ëÇØ¼ SQL Update ¹®À» ½ÇÇàÇÏ´Â ¿¹Á¦¸¦ ±¸ÇöÇÕ´Ï´Ù.
±¸Ã¼ÀûÀ¸·Î 'Colombian' ÁÖ°£ ÆÇ¸Å·®À» 75·Î coffees Å×À̺íÀÇ sales ÇʵåÀÇ °ªÀ» °»½ÅÇϰڽÀ´Ï´Ù.
UpdateCoffees.java ÆÄÀÏÀ» JDBC ÇÁ·Î±×·¡¹Ö ¼ø¼·Î ÄÚµùÇÕ´Ï´Ù.
- JDBC µå¶óÀ̹ö ·Îµù
- Connection ¸Î±â
- SQL ½ÇÇà
- [SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ ½ÇÇà°á°ú ó¸®]
- ÀÚ¿ø ¹Ýȯ
UpdateCoffees.java
import java.sql.*;
public class UpdateCoffees {
public static void main( String[] args ) {
Connection con;
Statement stmt;
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
try {
// 1. JDBC µå¶óÀ̹ö ·Îµù
Class.forName( "oracle.jdbc.driver.OracleDriver" );
} catch( java.lang.ClassNotFoundException e ) {
System.err.print( "ClassNotFoundException :" );
System.err.println( e.getMessage() );
}
try {
// 2. Connection ¸Î±â
con = DriverManager.getConnection( url, "scott", "tiger" );
stmt = con.createStatement();
// 3.SQL ½ÇÇà
String query = "UPDATE coffees " +
"SET sales = 75 " +
"WHERE cof_name LIKE 'Colombian'";
stmt.executeUpdate( query );
//5. ÀÚ¿ø ¹Ýȯ
con.close();
stmt.close();
} catch( SQLException ex ) {
System.out.println( "SQLException : " + ex.getMessage() );
}
}
}
ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇÕ´Ï´Ù.
Àü ¿¹Á¦¿Í ´Þ¸® À̹ø¿¡´Â SQLPLUS¿¡ Á¢¼ÓÇÏ¿© È®ÀÎÇÏÁö ¸»°í ¾Æ·¡ ¿¹Á¦¸¦ ´Ù½Ã ½ÇÇàÇÏ¿© Á¤¸»·Î 'Colombian' Ä¿ÇÇÀÇ ÆÇ¸Å·®(sales)ÀÌ 75°³·Î °»½ÅµÇ¾ú´ÂÁö È®ÀÎÇÕ´Ï´Ù.
¾Æ·¡ ¿¹Á¦´Â 'Colombian' ¶õ À̸§ÀÇ Ä¿ÇÇÀÇ ÆÇ¸Å·®À» fetch ÇØ¼ ±× ³»¿ëÀ» º¸¿©ÁÖ°í ÀÖ½À´Ï´Ù.
GetColombianSales.java
import java.sql.*;
public class GetColombianSales {
public static void main( String[] args ) {
Connection con;
Statement stmt;
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
try {
// 1. JDBC µå¶óÀ̹ö ·Îµù
Class.forName( "oracle.jdbc.driver.OracleDriver" );
} catch( java.lang.ClassNotFoundException e ) {
System.err.print( "ClassNotFoundException :" );
System.err.println( e.getMessage() );
}
try {
// 2.Connection ¸Î±â
con = DriverManager.getConnection( url, "scott", "tiger" );
// 3.SQL ½ÇÇà
stmt = con.createStatement();
String query = "SELECT cof_name, sales FROM coffees " +
"WHERE cof_name LIKE 'Colombian'";
// 4.[SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ ½ÇÇà°á°ú ó¸®]
ResultSet rs = stmt.executeQuery( query );
// cof_name ÀÌ 'Colombian' ÀÎ °ÍÀº coffees Å×ÀÌºí¿¡ ÇϳªÀ̹ǷÎ
// next()¸Þ¼Òµå¸¦ Çѹø¸¸ È£ÃâÇÑ´Ù.
rs.next();
String s = rs.getString(1);
int n = rs.getInt(2);
System.out.println(n + " pounds of " + s + " sold this week.");
// 5.ÀÚ¿ø¹Ýȯ
rs.close();
stmt.close();
con.close();
} catch( SQLException ex ) {
System.out.println( "SQLException : " + ex.getMessage() );
}
}
}
UpdateCoffees.java ¿Í GetColombianSales.java ¸¦ ¾Æ·¡¿Í °°ÀÌ Â÷·Ê´ë·Î ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇÕ´Ï´Ù.
D:\>javac UpdateCoffees.java D:\>java UpdateCoffees D:\>javac GetColombianSales.java D:\>java GetColombianSales 75 pounds of Colombian sold this week. D:\>
ÀÌÁ¦ ½ÇÇàÇÒ ¶§¸¶´Ù ¿À´Ã ¸ÅÃâ·®À» ´©ÀûµÇ¾î °»½ÅµÇµµ·Ï ÇÁ·Î±×·¥À» º¯°æÇØ º¸°Ú½À´Ï´Ù.
±¸Ã¼ÀûÀ¸·Î ½ÇÇàÇÒ ¶§¸¶´Ù Columbian Ä¿ÇÇÀÇ ¿À´Ã ÆÇ¸Å·®ÀÌ 5¾¿ ´©ÀûµÇ´Â ÇÁ·Î±×·¥ÀÔ´Ï´Ù.
UpdateColombianSales.java¸¦ Àü JDBC ÇÁ·Î±×·¡¹Ö ¼ø¼·Î ÄÚµùÇÕ´Ï´Ù.
- JDBC µå¶óÀ̹ö ·Îµù
- Connection ¸Î±â
- SQL ½ÇÇà
- [SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ Ã³¸®]
- ÀÚ¿ø ¹Ýȯ
UpdateColombianSales.java
import java.sql.*;
public class UpdateColombianSales {
public static void main( String[] args ) {
Connection con;
Statement stmt;
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
try {
// 1. JDBC µå¶óÀ̹ö ·Îµù
Class.forName( "oracle.jdbc.driver.OracleDriver" );
} catch( java.lang.ClassNotFoundException e ) {
System.err.print( "ClassNotFoundException :" );
System.err.println( e.getMessage() );
}
try {
// 2. Connection ¸Î±â
con = DriverManager.getConnection( url, "scott", "tiger" );
stmt = con.createStatement();
// 3. SQL ½ÇÇà
String updateString = "UPDATE coffees " +
"SET sales = sales + 5 " +
"WHERE cof_name LIKE 'Colombian'";
stmt.executeUpdate( updateString );
// 4. [SQL¹®ÀÌ select¹®À̾ú´Ù¸é ResultSetÀ» ÀÌ¿ëÇÑ ½ÇÇà°á°ú ó¸®]
String query = "SELECT cof_name, sales FROM coffees " +
"WHERE cof_name LIKE 'Colombian'";
ResultSet rs = stmt.executeQuery( query );
while ( rs.next() ) {
String s = rs.getString(1);
int n = rs.getInt(2);
System.out.println(n + " pounds of " + s + " sold to date.");
}
// 5. ÀÚ¿ø¹Ýȯ
rs.close();
stmt.close();
con.close();
} catch( SQLException ex ) {
System.out.println( "SQLException : " + ex.getMessage() );
}
}
}
¾Æ·¡¿Í °°ÀÌ ÄÄÆÄÀÏÇÏ°í ½ÇÇàÇÏ¸é ´©ÀûÀÌ µÇ´Â °ÍÀ» ¾Æ·¡¿Í °°ÀÌ º¸½Ç ¼ö ÀÖ½À´Ï´Ù.
D:\>javac UpdateColombianSales.java D:\>java UpdateColombianSales 5 pounds of Colombian sold to date. D:\>java UpdateColombianSales 10 pounds of Colombian sold to date. D:\>java UpdateColombianSales 15 pounds of Colombian sold to date. D:\>
- ´ÙÀ½ °Á : PreparedStatement
- ÀÌÀü °ÁÂ : SELECT
