create table STUDENT (ID number (6) NOT NULL, NAME NVARCHAR2 (m), age number (3), SEX number (1), birthday DATE)
public class Dbutil {private final String driver= "Oracle.jdbc.driver.OracleDriver";
Private final String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";
Private final String Username= "Scott";
Private final String userpwd= "Tiger";
Connection Conn=null;
PreparedStatement Pst=null;
ResultSet Rs=null;
Public Connection getconnection () throws SQLException, classnotfoundexception{if (conn==null) {class.forname (DRIVER);
Conn=drivermanager.getconnection (URL,USERNAME,USERPWD);
Return conn;
public int executeupdate (String sql,object[]params) throws SQLException, classnotfoundexception{getconnection ();
Pst=conn.preparestatement (SQL);
if (params!=null&¶ms.length!=0) {for (int i = 0; i < params.length i++) {pst.setobject (i+1, params[i]);
} int result=pst.executeupdate ();
return result; }
}
public void Save (Student stu) {
String sql= ' insert into Student (id,name,age,sex,birthday) VALUES (sq_student_ Id.nextval,?,?,?,?) ";
Object []params={stu.getname (), Stu.getage (), Stu.getsex (), Stu.getbirthday ()};
Dbutil dbutil=new dbutil ();
try {
dbutil.executeupdate (SQL, params);
} catch (SQLException e) {
e.printstacktrace ();
} catch ( ClassNotFoundException e) {
e.printstacktrace ();
} finally{
Dbutil.closeall ();
}
Scanner input=new Scanner (system.in);
System.out.print ("Please enter user name:");
String name=input.nextline ();
System.out.print ("Please enter Age:");
int Age=input.nextint ();
System.out.print ("Please enter gender (1: Male, 0: female):");
int Sex=input.nextint ();
System.out.print ("Please enter the date of birth (format: XXXX-XX-XX):");
Input.nextline ();
String birthdaystr=input.nextline ();
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Date Birthday=null;
Student Stu=null;
try {
birthday=sdf.parse (BIRTHDAYSTR);
Stu=new Student (name,age,sex,new java.sql.Date (Birthday.gettime ()));
catch (ParseException e) {
stu=new Student (name,age,sex);
}
Studentdao studao=new Studentdao ();
Save
Studao.save (Stu);
If you use the statement object, the following:
String sql= "INSERT into student (Id,name,age,sex,birthday) VALUES (Sq_student_id.nextval, '"
+stu.getname () + "'," + Stu.getage () + "," +stu.getsex () + ", To_date (' +stu.getbirthday () +" ', ' Yyyy-mm-dd ')) ";