📄 mydemo1.java
字号:
// usage: 1. compile: javac mydemo1.java
// 2. execute: java mydemo1
//
// This sample program illustrates how SQL queries, including updates, can be run in Java
//
import java.sql.*;
import java.math.*;
import java.io.*;
import java.awt.*;
public class mydemo1 {
public static void main (String args []) throws SQLException {
try
{
// Load Oracle driver
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
// Connect to the local database. Note that you should not hardcode the "userid" and "password" in your
// program. Instead, they should be read in from user input
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@groucho.cc.binghamton.edu:1521:ACAD92", "userid", "password");
// Query
Statement stmt = conn.createStatement ();
// Save result
ResultSet rset;
rset = stmt.executeQuery ("SELECT * FROM student");
// Print the result, one tuple in each iteration, student has 5 attributes as shown below:
// Name Null? Type
// ----------------------------------------- -------- ----------------------------
// SID CHAR(6)
// SNAME VARCHAR2(20)
// AGE NUMBER(2)
// GPA NUMBER(3,2)
// DEPTNAME VARCHAR2(20)
//
// You can copy this table from the instructor's account before run this program
// create table student as select * from meng.student;
//
while (rset.next ()) {
System.out.print (rset.getString (1)+" ");
System.out.print (rset.getString (2)+" ");
System.out.print (rset.getString (3)+" ");
System.out.print (rset.getString (4)+" ");
System.out.println (rset.getString (5)+" ");
}
//create and execute an update
String updateString = "UPDATE student SET age = 25 WHERE sid = 's00001'";
stmt.executeUpdate(updateString);
//Insert a new tuple into the student table, ? are placeholder for input values
PreparedStatement insert = conn.prepareStatement("INSERT into student VALUES(?,?,?,?,?)");
// Input sid from keyboard
BufferedReader readKeyBoard;
String sid;
readKeyBoard = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter SID:");
sid = readKeyBoard.readLine();
insert.setString(1, sid);
// Input other values
insert.setString(2, "Joe");
insert.setInt(3, 19);
insert.setInt(4, 4);
insert.setString(5, "Bio");
// execute the insertion as an update
insert.executeUpdate();
//Query again.
rset = stmt.executeQuery ("SELECT * FROM student");
// Print again to see if the above changes have been made
while (rset.next ()) {
System.out.print (rset.getString (1)+" ");
System.out.print (rset.getString (2)+" ");
System.out.print (rset.getString (3)+" ");
System.out.print (rset.getString (4)+" ");
System.out.println (rset.getString (5)+" ");
}
//close the result set, statement, and the connection
rset.close();
stmt.close();
conn.close();
}
catch (SQLException ex) { System.out.println ("\n*** SQLException caught ***\n");}
catch (Exception e) {System.out.println ("\n*** other Exception caught ***\n");}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -