Oracle database first creates a table and adds some data
1. First create a student table in the Oracle database:
CREATE TABLE Student
(
ID number (one) not null primary key,
stu_name varchar is not NULL, gender number
(1 1) default NULL, age number
(one) default NULL, address
varchar (128) default null
);
2. Add some data to the table
insert into student values('1','王小军','1','17','北京市和平里七区30号楼7门102')
Writing Java code in MyEclipse
1. Import Ojdbc6.jar into the project
Create an item first, then right-click on the item to-->new-->folder;folder Name:lib, create a folder lib in the project, and import the Ojdbc6.jar package into the folder
The package download address link: Http://wd.jb51.net:81//201612/yuanma/ojdbc6_jb51.rar
Move the mouse over the packet; right--->build path-->add to build path;
2. Create a class, start encoding
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException; public class Operateoracle {///192.168.0.X is the native address (to be changed to its own IP address), the 1521 port number, XE is the default database name for Lite Oracle private static S
Tring Usernamr = "ORCL";
private static String PASSWORD = "ORCL";
private static String Drvier = "Oracle.jdbc.OracleDriver";
private static String URL = "Jdbc:oracle:thin:@192.168.0.x:1521:xe";
Create a database connection Connection Connection = null;
Create precompiled statement objects, usually using this without statement preparedstatement pstm = null;
Create a result set object ResultSet rs = null;
/** * Add data to the database * First gets the total number of data in the table, total +1 is the ID value of the new data * @param stuname: Student name * @param gender: Students ' gender, 1 for male, 2 for female * @param age: Student ages * @param address: Student Home */public void AddData (string stuname, int gender, int age, String address) {connection = Getcon
Nection ();
String sql =//"INSERT into student values (' 1 ', ' Wang Xiaojun ', ' 1 ', ' 17 ', ' Building 30th, seven, Beijing, China 7 ')"; String sql = "SELECT COUNT (*) from student where 1 = 1";
String sqlstr = "INSERT into student values (?,?,?,?,?)";
int count = 0;
The total number of data in the student table of the try {///COMPUTE Database pstm = connection.preparestatement (sql);
rs = Pstm.executequery ();
while (Rs.next ()) {count = Rs.getint (1) + 1;
System.out.println (Rs.getint (1));
//Execute Insert data operation pstm = Connection.preparestatement (SQLSTR);
Pstm.setint (1, count);
Pstm.setstring (2, stuname);
Pstm.setint (3, gender);
Pstm.setint (4, age);
Pstm.setstring (5, address);
Pstm.executeupdate ();
catch (SQLException e) {e.printstacktrace ();
finally {Releaseresource (); /** * Delete data to database * @param stuname: Delete data by name/public void DeleteData (String stuname) {connection = Getconnectio
N ();
String sqlstr = "Delete from student where stu_name=?";
System.out.println (Stuname);
try {//execute delete data operation pstm = Connection.preparestatement (SQLSTR);
Pstm.setstring (1, stuname);
Pstm.executeupdate ();
catch (SQLException e) {e.printstacktrace (); } finally {Releaseresource (); /** * Modify data to Database * @param stuname: Student name, query for a row value to modify based on this value * @param gender * @param age * @param address/Public V
OID UpdateData (string stuname, int gender, int age, String address) {connection = getconnection ();
String sql = "SELECT ID from student where 1 = 1 and stu_name =?"; String sqlstr = "Update student set stu_name=?,gender=?,age=?,address=?"
where id=? ";
int count = 0;
The total number of data in the student table of the try {///COMPUTE Database pstm = connection.preparestatement (sql);
Pstm.setstring (1, stuname);
rs = Pstm.executequery ();
while (Rs.next ()) {count = Rs.getint (1);
System.out.println (Rs.getint (1));
//Execute Insert data operation pstm = Connection.preparestatement (SQLSTR);
Pstm.setstring (1, stuname);
Pstm.setint (2, gender);
Pstm.setint (3, age);
Pstm.setstring (4, address);
Pstm.setint (5, count);
Pstm.executeupdate ();
catch (SQLException e) {e.printstacktrace ();
finally {Releaseresource (); }/** * Query data to database */public void Selectdata () {connection = GetconnEction ();
String sql = "SELECT * from student where 1 = 1";
try {pstm = connection.preparestatement (sql);
rs = Pstm.executequery ();
while (Rs.next ()) {String id = rs.getstring ("id");
String name = rs.getstring ("Stu_name");
String gender = rs.getstring ("gender");
String age = rs.getstring (' age ');
String address = rs.getstring ("Address");
System.out.println (id + "T" + name + "T" + gender + "\ T" + Age + "T" + address);
} catch (SQLException e) {e.printstacktrace ();
finally {Releaseresource ();
}/** * uses ResultSetMetaData to compute the number of columns */public void SelectData2 () {connection = getconnection ();
String sql = "SELECT * FROM Employees WHERE 1 = 1";
int count = 0;
try {pstm = connection.preparestatement (sql);
rs = Pstm.executequery ();
while (Rs.next ()) {count++;
} ResultSetMetaData RSMD = Rs.getmetadata ();
int cols_len = Rsmd.getcolumncount ();
System.out.println ("count=" + Count + "\tcols_len=" + Cols_len); The catch (SQLException e) {e.printstacktrace ();
finally {Releaseresource ();
/** * Get Connection Object * * @return/public Connection getconnection () {try {class.forname (drvier);
Connection = Drivermanager.getconnection (URL, USERNAMR, PASSWORD);
SYSTEM.OUT.PRINTLN ("Successful Connection Database");
catch (ClassNotFoundException e) {throw new RuntimeException ("Class not find!", e);
catch (SQLException e) {throw new RuntimeException ("Get Connection error!", e);
return connection;
/** * FREE resources/public void Releaseresource () {if (rs!= null) {try {rs.close ();
catch (SQLException e) {e.printstacktrace ();
} if (pstm!= null) {try {pstm.close ();
catch (SQLException e) {e.printstacktrace ();
} if (connection!= null) {try {connection.close ();
catch (SQLException e) {e.printstacktrace ();
}
}
}
}
3. Create a test class
public class Test {public
static void Main (string[] args) {
/**
* Add or remove changes, but there are some limitations
* 1. The problem is not big
* 2. Delete Out a value to delete (possible value does not exist--> No processing mechanism, the value is not unique how to deal with?)
* 3. Question of deletion
* 4. Check the problem is not big//
/Create Operateoracle object
operateoracle oo=new operateoracle ();
Test adds data operation
//oo. AddData ("Sun Yat-sen", 1, 25, "No. 111th, Hongqi Road, Haidian District, Beijing");
Test Delete data Operation
//oo. DeleteData ("Sun Yat-sen");
Test the update data operation
OO. UpdateData ("Sun Yat-sen", 1, 30, "No. 11th, Yue Shan Road, Dongcheng District, Beijing");
Test query data Operation
//oo. Selectdata ();
Test ResultSetMetaData class
//oo. SelectData2 ();
}
As noted in the test class, only the correct way to connect to the Oracle database, operation and deletion to check operations, but for some error-handling mechanism is not perfect.
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!