import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
/**
*
* @author Arvind
*/
public class dao {
Connection con;
PreparedStatement stat;
ResultSet rs;
String insert = "insert into milk(id,P_date,Quantity,rate,Milk_type,company) values(?,?,?,?,?,?)";
public int insert_record(data mk) throws ClassNotFoundException, SQLException {
con = getConnection();
stat = con.prepareStatement(insert);
int id = objectid();
stat.setInt(1, id);
System.out.println("id set" + id);
System.out.println("date is" + mk.getBought());
stat.setString(2, mk.getBought());
System.out.println("bought set" + id);
stat.setDouble(3, mk.getQuantity());
System.out.println("quantity set" + id);
stat.setDouble(4, mk.getRate());
System.out.println("rate set" + id);
stat.setString(5, mk.getType());
System.out.println("type set" + id);
stat.setString(6, mk.getCompany());
System.out.println("company set" + id);
int i = stat.executeUpdate();
return i;
}
String get_id = "select id from objectid";
private int objectid() throws ClassNotFoundException, SQLException {
int i = 0;
con = getConnection();
stat = con.prepareStatement(get_id);
rs = stat.executeQuery();
while (rs.next()) {
i = rs.getInt("id");
}
update(i);
return i;
}
String update_id = "update objectid set id=?";
private void update(int i) throws ClassNotFoundException, SQLException {
con = getConnection();
stat = con.prepareStatement(update_id);
stat.setInt(1, ++i);
}
private Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:milk", "milk", "milk");
return conn;
}
public java.sql.Date utilToSqlDate(Date da) {
return new java.sql.Date(da.getTime());
}
}