Want to Take The Diagonal Elements From A Square Matrix
28 04 2007Myself and My Colleague were doing some bug fix, simultaneously we were writing a simple JDBC code, at that time my colleague caught up something interesting.
Yes if you ever wanted to print out or take out the diagonal elements from the Square Matrix, bear it in mind the code given below is applicable only for the Square Matrix (To Know More about Square Matrix) .
For running the below code I used MySQL - Version 5.0.16 as the Database and Eclipse SDK - Version: 3.2.2.
Also do replace the following: DB_USER_NAME, DB_PASSWORD, DB_NAME, DB_TABLE_NAME.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class JdbcGetRowTest {
public static void main(String[] args) {
MysqlDataSource mysqlDataSrc = null;
if (mysqlDataSrc == null) {
mysqlDataSrc = new MysqlDataSource();
mysqlDataSrc.setServerName(”localhost”);
mysqlDataSrc.setPortNumber(3306);
mysqlDataSrc.setDatabaseName(”DB_NAME”);
mysqlDataSrc.setUser(”DB_USER_NAME”);
mysqlDataSrc.setPassword(”DB_PASSWORD”);
}
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String query = “select * from DB_TABLE_NAME”;
try {
con = mysqlDataSrc.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString(rs.getRow()));
}
} catch (SQLException sqlE) {
sqlE.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}





























