It's the first time I've posted here, but i'm really in a trouble.
I have the table EMPLOYEES
:
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)
and I need to insert the next record:
INSERT INTO employees VALUES (307,'Alfredo','Garcia','alfredgad','222222', TO_DATE('07-JUN-1994', 'dd-MON-yyyy'),'AC_ACCOUNT',8300,NULL,205,110);
I have the next code:
My class Conexion
package nomina;
import java.io.*;
import java.sql.*;
import javax.swing.*;
public class Conexion {
static Connection conn=null;
static Statement st=null;
static ResultSet rs=null;
static String bd="ORCL";
static String login="XXXX";
static String password="XXX";
static String url="jdbc:oracle:thin:@localhost:1521:ORCL";
public static Connection Enlace(Connection conn)throws SQLException {
try {
Class.forName("oracle.jdbc.OracleDriver");
conn=DriverManager.getConnection(url, login, password);
}
catch(ClassNotFoundException e )
{
System.out.print("Clase no encontrada");
}
return conn;
}
public static Statement sta(Statement st)throws SQLException {
conn=Enlace(conn);
st=conn.createStatement();
return st;
}
public static ResultSet EnlEst(ResultSet rs)throws SQLException {
st=sta(st);
rs=st.executeQuery("select * from employees");
return rs;
}
}
The method where I'm trying to insert the record:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int id = 0;
double salario = 0;
int jefe = 0;
int depto = 0;
String nombre = "", apellidos = "", correo = "", telefono = "", puesto = "";
id = Integer.parseInt(jTextIdEmpleado.getText());
apellidos = jTextNom.getText();
apellidos = jTextApe.getText();
correo = jTextMail.getText();
telefono = jTextTel.getText();
puesto = jTextPuesto.getText();
salario = Double.parseDouble(jTextSalario.getText());
jefe = Integer.parseInt(jTextIdJefe.getText());
depto = Integer.parseInt(jTextDep.getText());
try {
// main miconexion = new main();
conn = Conexion.Enlace(conn);
Statement sta = conn.createStatement();
try {
sta.executeQuery("INSERT INTO employees VALUES (307,'Alfredo','Garcia','alfredgad','222222', TO_DATE('07-JUN-1994', 'dd-MON-yyyy'),'AC_ACCOUNT',8300,NULL,205,110)");
} catch (SQLException e) {
System.out.println("error");
sta.close();
}
String sqlinsertar = "insert into employees (employee_id,first_name,last_name,email,phone_number,hire_date,job_id,salary,commission_pct,manager_id,department_id) values (?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement psta = conn.prepareStatement(sqlinsertar);
psta.setInt(1, id);
psta.setString(2, nombre);
psta.setString(3, apellidos);
psta.setString(4, correo);
psta.setString(5, telefono);
psta.setDate(6, (Date) jDateChooser1.getDate());//Probar por separado
psta.setString(7, puesto);
psta.setDouble(8, salario);
psta.setNull(9, java.sql.Types.INTEGER);
psta.setInt(10, jefe);
psta.setInt(11, depto);
psta.executeUpdate();
psta.close();
JOptionPane.showMessageDialog(null, "Registro Guardado Satisfactoriamente");
} catch (Exception e) {
System.out.println(e.getCause());
}
}
But when I execute it, it doesn't work and the console only says "null". I think the problem is either the date or the null but I've been trying for a week and I haven't worked it out.
I hope someone can tell my what the problem is.
Thanks in advance
Regarding,
next error java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
It looks like JFileChooser returns a java.util.Date and what you need is a java.sql.Date, a class that inherits from java.util.Date and that is used by SQL databases as a Date type. I don't see any copy constructor, but perhaps you can use the constructor that takes a long parameter to convert from java.util.Date to java.sql.Date.
So what if you change this:
psta.setDate(6, (Date) jDateChooser1.getDate());//Probar por separado
to this:
java.util.Date utilDate = (java.util.Date) jDateChooser1.getDate();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
psta.setDate(6, sqlDate);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.