package org.com.db;
import java.sql.*;
public class database {
static // 驱动程序就是之前在classpath中配置的jdbc的驱动程序的jar包中
     String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
// 连接地址是由各个数据库生产商单独提供的,所以需要单独记住
 static String DBURL = "jdbc:oracle:thin:@localhost:1521:hy";
// 连接数据库的用户名
 static String DBUSER = "scott";
// 连接数据库的密码
     static String DBPASS = "tiger";
     Connection conn=null;
     Statement stmt = null;
     ResultSet rs = null;
public database() throws Exception 
{ Class.forName(DBDRIVER);
}
public Connection getConnection() throws Exception {
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
return conn;
} public void closeResource()
{
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}
} public ResultSet executeQuery(String sql) throws Exception { conn = DriverManager.getConnection(DBURL, DBUSER,DBPASS );
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
return rs; } public int executeUpdate(String sql) throws Exception {
int result = 0;
try {
conn = DriverManager.getConnection(DBURL, DBUSER,DBPASS);
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
return result;
} finally {
closeResource(); 
}
}
//public static void main(String[] args) throws Exception {
// Connection conn = null; // 表示数据库的连接的对象
// // 1、使用Class类加载驱动程序
// Class.forName(DBDRIVER);
// // 2、连接数据库
// conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
// System.out.println(conn);
// // 4、关闭数据库
// conn.close();
// }
}
package org.com.emp;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.com.db.database;
public class emplydesin {
public int emplyadd(int EmpPassword, String Name,
String Cellphone, String Address, String Gender, String Birthspace,
String Nation, Date Birthtime, String Title, int DeptID,
int DutiesID, String Wphone, String Fphone, String Email,
String Political, String Health, String Profession, String Gegree,
String Marry, String Pincodes, String Icom, int PermissionID,
Date Luru_data, Date Modifydata) {
int rows = 0;
Connection conn = null; 
PreparedStatement pstmt = null;
String sql = "insert into emply(EmployeeID,EmpPassword,Name,Cellphone,Address,Gender,Birthspace,Nation,Birthtime,Title,DeptID,DutiesID,Wphone,Fphone,Email,Political,Health,Profession," +
"Gegree,Marry,Pincodes,Icom,PermissionID,Luru_data,Modifydata)values(book_sequence.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
database db = null;
try {
db = new database();
pstmt=conn.prepareStatement(sql);
pstmt.setInt(2, EmpPassword);
pstmt.setString(3, Name);
pstmt.setString(4, Cellphone);
pstmt.setString(5, Address);
pstmt.setString(6, Gender);
pstmt.setString(7, Birthspace);
pstmt.setString(8, Nation);
pstmt.setDate(9, new java.sql.Date(Birthtime.getTime()));
pstmt.setString(10, Title);
pstmt.setInt(11, DeptID);
pstmt.setInt(12, DutiesID);
pstmt.setString(13, Wphone);
pstmt.setString(14, Fphone);
pstmt.setString(15, Email);
pstmt.setString(16, Political);
pstmt.setString(17, Health);
pstmt.setString(18, Profession);
pstmt.setString(19, Gegree);
pstmt.setString(20, Marry);
pstmt.setString(21, Pincodes);
pstmt.setString(22, Icom);
pstmt.setInt(23, PermissionID);
pstmt.setDate(24, new java.sql.Date(Luru_data.getTime()));
pstmt.setDate(25,  new java.sql.Date(Modifydata.getTime()));

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// database db = new database();
rows = db.executeUpdate(sql); } catch (Exception e) {
System.out.print(e);
}
return rows;
}
public static void main(String arg[]){

emplydesin em=new emplydesin();


int ro=em.emplyadd(123,
"丫丫","151999999","宜","w","宜春","汉","1994-12-23","经理",2001,
3001,"77777777","8888888","[email protected]","群众","良好","计算机","本科","未婚",
"36050288888888","照片1",4001,todate(03-12-81),03-12-81);?不知道怎么表达 }}
麻烦看看如何插入正确的测试数据,特别是时间的数据,数据库表如下create sequence emplyseq start with 1001;
create table emply
(
   EmployeeID number(4)  primary key not null,
  EmpPassword number(20) not null,
   Name     varchar2(10)  not null,
   Cellphone  varchar2(20)  not null,
   Address    varchar2(10)  not null,
   Gender    varchar2(10) not null,
   Birthspace   varchar2(10) not null,
   Nation    varchar2(10) not null,
   Birthtime   date not null,
   Title    varchar2(10) not null,
   DeptID    number(4) not null,
   DutiesID    number(4) not null,
   Wphone    varchar2(20) not null,
   Fphone   varchar2(20),
   Email   varchar2(20),
   Political       varchar2(5) not null,
   Health  varchar2(5) not null,
   Profession  varchar2(10) not null,
  Gegree     varchar2(5) not null,
   Marry    varchar2(5) not null,
   Pincodes    varchar2(20)  not null,
   Icom    varchar2(10),
   PermissionID number(4) not null,
   Luru_data  date,
   Modifydata  date)
麻烦看看,虽然很长,但核心的就那些,麻烦帮我调试下,谢谢!!!!

解决方案 »

  1.   


    --看得头晕,to_date()格式化时间
    insert into emply
    values(...to_date('2011-4-19','yyyy-mm-dd')...);
      

  2.   

    1.  MONTHS_BETWEEN with to_date function   
    SQL>
    SQL> SELECT MONTHS_BETWEEN(TO_DATE('22SEP2006','ddMONyyyy'),
      2    TO_DATE('13OCT2001','ddMONyyyy')) "Months difference"
      3  FROM dual;Months difference
    -----------------
           59.2903226SQL>
    SQL>
    SQL>2.  Converting spelled date to DATE format   
    SQL>
    SQL> -- Converting spelled date to DATE format.
    SQL> SELECT TO_DATE('January 15','MONTH DD') "Sample" from DUAL;Sample
    ---------
    15-JAN-06SQL>3.  Converting number representation to DATE format   
    SQL>
    SQL> -- Converting number representation to DATE format.
    SQL> SELECT TO_DATE('061167','MMDDYY') "Birthday" from DUAL;Birthday
    ---------
    11-JUN-67SQL>4.  TO_CHAR(TO_DATE('04-JUL-15', 'DD-MON-YY'), 'DD-MON-YYYY')   
    SQL>
    SQL> SELECT TO_CHAR(TO_DATE('04-JUL-15', 'DD-MON-YY'), 'DD-MON-YYYY'),
      2         TO_CHAR(TO_DATE('04-JUL-75', 'DD-MON-YY'), 'DD-MON-YYYY')
      3  FROM dual;TO_CHAR(TO_ TO_CHAR(TO_
    ----------- -----------
    04-JUL-2015 04-JUL-2075SQL>
               5.  TO_DATE('04-JUL-15', 'DD-MON-RR'): Uses the RR format when interpreting the years 15 and 75 
    SQL>
    SQL> --Uses the RR format when interpreting the years 15 and 75
    SQL>
    SQL> SELECT
      2    TO_CHAR(TO_DATE('04-JUL-15', 'DD-MON-RR'), 'DD-MON-YYYY'),
      3    TO_CHAR(TO_DATE('04-JUL-75', 'DD-MON-RR'), 'DD-MON-YYYY')
      4  FROM dual;TO_CHAR(TO_ TO_CHAR(TO_
    ----------- -----------
    04-JUL-2015 04-JUL-1975SQL>
      

  3.   

    没回答上,我的意思是说
    int ro=em.emplyadd(123,
    "丫丫","151999999","宜","w","宜春","汉","1994-12-23","经理",2001,
    3001,"77777777","8888888","[email protected]","群众","良好","计算机","本科","未婚",
    "36050288888888","照片1",4001,todate(03-12-81),03-12-81);
    我应该写那些数据来测试,日期的每次都不对
      

  4.   

    todate('1981-03-12','YYYY-MM-DD');