Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get the total row count of the table. I'm trying methods of ResultSetMetaData, but it only have getColumnCount(). Is there any other way of getting total count of rows.
I'm trying following code,
Java
public void Save()
	{
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost/shri","root","");
			
			//Getting next ID

			int id=1;
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select id from mydb");
while(rs.next()!=false)
			{
				id++;
			} // I tried this another method but doesn't worked

			ResultSetMetaData rsmd = rs.getMetaData();
			id=rsmd.getColumnCount(); // Here it is

			//Saving Data
			PreparedStatement ps = con.prepareStatement("Insert into mydb(id,fname,mname,lname,nickname,email)values(?,?,?,?,?,?)");
			ps.setInt(1,id);
			ps.setString(2,txtFirstName.getText());
			ps.setString(3,txtMiddleName.getText());
			ps.setString(4,txtLastName.getText());
			ps.setString(5,txtNickname.getText());
			ps.setString(6,txtEmail.getText());
			ps.execute();		
			lblStatus.setText("Saved.......");
			clear();
		}
		catch(Exception e)
		{
			System.out.println (e);
			lblStatus.setText(e.toString());
		}
	}
Posted

 
Share this answer
 
Comments
PIEBALDconsult 18-Dec-14 21:55pm    
There is probably a correct way to do that.
DamithSL 18-Dec-14 22:25pm    
if it is auto_increment you don't need to give it when insert, database will handle that.
remove id from insert like below
PreparedStatement ps = con.prepareStatement("Insert into mydb(fname,mname,lname,nickname,email)values(?,?,?,?,?)");
and don't set id
//ps.setInt(1,id);
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900