timestamp issue in mysql - mysql

While executing the following query in querybrowser i;m getting correct date.But in jdbc code resultset is returning incorrect date.
pstmt = con.prepareStatement("select DATE(sih.loaded_date) from tab_ats_sellthroughloader_history sih order by loaded_date desc limit 1");
rs = pstmt.executeQuery();
java.sql.Timestamp saveDate = null;
while (rs.next()) {
saveDate = rs.getTimestamp(1);
}
System.out.println("result:::::::::::::::::"+saveDate);

Related

MySQL LIMIT with OFFSET not working as expected

PreparedStatement ps is working as expected with user_type being updated for the first row however, my second statement (ps1) which should update the second row isn't. As the only change is the offset 'LIMIT 1, 1;' I can only assume that's why the system throws a MYSQLSyntaxErrorException. I'm new to MySQL so unsure why it's throwing an exception and how to achieve the same by trying something else.
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/child_timer", "root", "");
PreparedStatement ps = conn.prepareStatement("UPDATE user_profile SET user_type=? ORDER BY user_id ASC LIMIT 1;");
ps.setString(1, sp.getUserType().name());
int x = ps.executeUpdate();
if (x > 0) {
alertDialogBuilder(AlertType.INFORMATION, "Success", null, "Changes have been successfully saved.");
}
PreparedStatement ps1 = conn.prepareStatement("UPDATE user_profile SET user_type=? ORDER BY user_id ASC LIMIT 1, 1;");
ps1.setString(1, sp.getUserType1().name());
int x1 = ps1.executeUpdate();
if (x1 > 0) {
alertDialogBuilder(AlertType.INFORMATION, "Success", null, "Changes have been successfully saved.");
} catch (Exception e1) {
System.out.println(e1);
}
Theres no offset in a MySQL Update LIMIT statement.
Provide a minimum id to create an offset instead like so:
wHERE user_id >= :offset

The returned value of resultset to get the object is null

this is my code.
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("select age from filesearch");
System.out.println(rs.next());//The return value is: null
if(rs.next()) {
System.out.println(rs.getObject("age"));
}
This is the variable in my database tables:
Listing:manager,age
data type:varchar,int
length:100,100
default:D:\Java\manager,10
According to the documentation for ResultSet#next(), this method returns a boolean, so you should never be seeing null from your print statement. That being said, you are not using the JDBC API correctly, and you should be using a pattern similar to this:
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("select age from filesearch");
while (rs.next()) {
System.out.println(rs.getObject("age"));
}

How to write query for count of a column in jdbc

I have a table attendance in which columns are id,name,date,status.Here what's the my problem is,If i select two dates(start date and end date) in my html page,I want to generate a attendance report of a student between the range of dates with name also no of present days also.How to write query for selecting name of the student and count of present days of particular student.For all help thanks in advance.
i wrote query like below.
String sql = "select student_name,count(status) from attendance where attendance.date>=? and attendance.date<=? and attendance.status=?";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test_schema", "root", "root");
String sql = "select student_name from attendance where attendance.date>=? and attendance.date<=? and attendance.status=?";
PreparedStatement pstmt = con.prepareStatement(sql);
//pstmt.setString(1, "Present");
pstmt.setString(1, stDate);
pstmt.setString(2, enDate);
pstmt.setString(3, "Present");
rs = pstmt.executeQuery();
while (rs.next()) {
java.sql.ResultSetMetaData rsmd=rs.getMetaData();
int colCount = rsmd.getColumnCount();
System.out.println("colCount :" + colCount);
System.out.println("count>>"+colCount);
StudentMark ge = new StudentMark();
ge.setStudentName(rs.getString(1));
stList.add(ge);
}
out.print(stList);
out.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
You can use the ResultSetMetaData to get the column count for any select query's result.
ResultSet rs= ... //Statement's result.
ResultSetMetaData rsmd= rs.getMetaData();
int colCount = rsmd.getColumnCount();

retrieving every attribute of every column from every row of a table mysql java

So far I know that it's possible to select the nth row from a table using the following code SELECT * FROM table ORDER BY column_name LIMIT n-1,1 ; In the following code I am trying to retrieve every attribute of every column from every row of a table named responsable which has the following columns :id,name,surname,mail,password. id is an integer while the other columns' type is string
java.sql.Connection connection = null;
int rowCount;
try {connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name","root","");
Statement stmt = null;
ResultSet result = null;
ResultSet rs = null;
rs = stmt.executeQuery("SELECT COUNT(*) FROM responsable");
rowCount = rs.getInt(1);
if(rowCount!=0)
{
for(int i=0;i<rowCount;i++)
{result= stmt.executeQuery("SELECT * FROM responsable ORDER BY id LIMIT"+i+",1");
System.out.put(result.getInt(1));
System.out.put(result.getString(2));
System.out.put(result.getString(3));
System.out.put(result.getString(4));
System.out.put(result.getString(5));}
}
}catch (Exception ex) {
out.println("Unable to connect to database");
}
I am trying to execute this code but I am getting no result.
Any help would be appreciated.
Your stmt variable is null. You need to create the statement first.
Statement stmt = connection.createStatement();
Also, you need to call next() to retrieve the next row from your resultset.
E.g.
if (rs.next()) {
rowCount = rs.getInt(1);
}
....
if (result.next()) {
System.out.put(result.getInt(1));
System.out.put(result.getString(2));
System.out.put(result.getString(3));
System.out.put(result.getString(4));
System.out.put(result.getString(5));
}

MySQL Jdbc convert string to date

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// date here is a string of format yyyy-MM-dd
java.util.Date date_1 = df.parse(date) ;
java.sql.Date sqldate = new java.sql.Date(date_1.getTime());
sql = "select * from fgs_stock_report where Report_date = ? ";
PreparedStatement two = con.prepareStatement(sql);
two.setDate(1,sqldate);ResultSet rs ;
rs = two.executeQuery(sql) ;
Here I get a Java Sql Exception asking for the right syntax near? . I am a beginner and I searched a lot for a solution but couldnt find. Please help me.
I think I see the problem, you are using a Statement.executeQuery(String) but you want PreparedStatement.executeQuery() - that is.
PreparedStatement two = con.prepareStatement(sql); // <-- Prepare a Statement.
two.setDate(1,sqldate); // <-- bind the parameter.
ResultSet rs ;
rs = two.executeQuery(sql) ; // <-- throw it away and use raw sql
What you want is,
ResultSet rs = two.executeQuery(); // <-- I'd use one line