0% found this document useful (0 votes)
6 views2 pages

Java Database Insert and Update Code

The document contains Java code snippets for inserting and updating records in a database table named 'student2'. It establishes a connection to an Oracle database, retrieves user input from text fields, and executes SQL statements to add or modify student records. Error handling is included for potential exceptions related to database connectivity and SQL execution.

Uploaded by

czxc650
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Java Database Insert and Update Code

The document contains Java code snippets for inserting and updating records in a database table named 'student2'. It establishes a connection to an Oracle database, retrieves user input from text fields, and executes SQL statements to add or modify student records. Error handling is included for potential exceptions related to database connectivity and SQL execution.

Uploaded by

czxc650
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

--insert--

try {
int id = [Link]([Link]());
String name = [Link]();
String phone = [Link]();
String email= [Link]();
int age = [Link]([Link]());

[Link]("[Link]");
Connection c =
[Link]("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");

String sql = "insert into student2 VALUES(?,?,?,?,?)";


PreparedStatement stmt = [Link](sql);
[Link](1, id);
[Link](2, name);
[Link](3, phone);
[Link](4, email);
[Link](5, age);

int rows = [Link]();


[Link](rows + "added");
[Link]();
[Link]();
} catch (ClassNotFoundException ex) {
[Link]([Link]());
} catch (SQLException ex) {
[Link]([Link]());
}
}
-----------------------------------------------------
--update--
try {
int id = [Link]([Link]());
String name = [Link]();
String phone = [Link]();
String email = [Link]();
int age = [Link]([Link]());

[Link]("[Link]");
Connection c =
[Link]("jdbc:oracle:thin:@localhost:1521:XE", "hr", "hr");

String sql = "update student2 set"


+ " name = ? , phone = ? ,"
+ "email = ? ,age = ?"
+ "where id = ?";
PreparedStatement stmt = [Link](sql);
[Link](1, name);
[Link](2, phone);
[Link](3, email);
[Link](4, age);
[Link](5, id);
int rows = [Link]();
[Link](rows + "added");
[Link]();
[Link]();
} catch (ClassNotFoundException ex) {
[Link]([Link]());
} catch (SQLException ex) {
[Link]([Link]());
}
}

You might also like