JDBC Inserts a date with PreparedStatement in Oracle

Source: Internet
Author: User
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 ')) ";






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.