I created object in global as.
public Object[] columns2;
I am performing some operation in code as:
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM "+Gtest+"");
ResultSetMetaData rsmd = rs.getMetaData();
NumOfCol = rsmd.getColumnCount();
for(int n=0;n<NumOfCol;n++)
{
columns2[n]=rsmd.getColumnName(n+1);
}
as columns2 should be like
Object[] columns2={"FirstName","LastName","Age"};
But it is giving nullpointerexception in for loop.
What changes should be done?
You must initiate columns2 first before push there data.
columns2 = new Object[NumOfCol];
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM "+Gtest+"");
ResultSetMetaData rsmd = rs.getMetaData();
NumOfCol = rsmd.getColumnCount();
columns2 = new Object[NumOfCol];
for(int n=0;n<NumOfCol;n++)
{
columns2[n]=rsmd.getColumnName(n+1);
}
You have not initialized columns2. You could change your code as follows:
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM "+Gtest+"");
ResultSetMetaData rsmd = rs.getMetaData();
NumOfCol = rsmd.getColumnCount();
columns2 = new Object[NumOfCol];
for(int n=0;n<NumOfCol;n++)
{
columns2[n]=rsmd.getColumnName(n+1);
}
This should make your code work. Although i would look into other Data Structures like an Arraylist if things like performance are important to you.
Related
I try to receive content of tables using Java code. This is the basic code and I have an issue with it. I need the code to be flexible so it can read different tables (with different amount of rows/columns etc). I want the while loop to print all the columns in a particular table.
public class Main1 {
public static void main(String[] args) throws Exception {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql:address", "root", "");
String SQL = "select * from users";
ResultSet rs = Statement.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("querying SELECT * FROM users");
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue + " " + rsmd.getColumnName(i));
}
System.out.println("");
}
}
}
OK, you are probably better off using a PreparedStatement
consider
PreparedStatement ps = conn.prepareStatement (SQL);
rs = ps.executeQuery ();
i have created jtable but it doesn't show the last column name i don't know what i did wrong in code database have 4 columns id , name, fathername and phone number but jtable only show 3 columns.
public void load() {
try {
DBO db = new DBO();
con = db.connect();
String sql = "Select * from personinfo";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
DefaultTableModel tb = new DefaultTableModel();
Vector col = new Vector();
for (int i = 1; i < count; i++) {
col.addElement(rsmd.getColumnName(i));
}
tb.setColumnIdentifiers(col);
while (rs.next()) {
Vector rows = new Vector();
for (int j = 1; j < rsmd.getColumnCount(); j++) {
rows.addElement(rs.getString(j));
}
tb.addRow(rows);
PersonTable.setModel(tb);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
I think you should use
pst.setString(1, "%"+name.getText()+"%");
when creating a prepared statement ? is used to replace bind variables. Values which you are not using as bind variables cannot be used the way you want, in this case using like. You can most probably go through this PreparedStatement IN clause alternatives?
Your question is quiet easily a duplicate Using “like” wildcard in prepared statement
I have following code,
private void trans_tabMouseClicked(java.awt.event.MouseEvent evt) {
try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online_store","poornima","mit1234");
if(con != null){
String query = "SELECT * FROM bill";
rs = stmt.executeQuery(query);
System.out.println("fffgg");
ResultSetMetaData rsmt = rs.getMetaData();
int c = rsmt.getColumnCount();
Vector row = new Vector();
DefaultTableModel model = (DefaultTableModel)expense_table.getModel();
while(rs.next())
{
row = new Vector(c);
for(int i = 1; i <= c; i++)
{
row.add(rs.getString(i));
}
model.addRow( row );
}
}
}catch(Exception ex){
System.out.println(ex);
}
}
in the above code query is not executed. Fired null pointer exception at rs = stmt.executeQuery(query); line. I tried hours, but I can't figure out where the issue is. Please help.
because stmt is null.
use this
String query = "SELECT * FROM bill";
stmt = con.createStatement();
rs = stmt.executeQuery(query);
instead of this
String query = "SELECT * FROM bill";
rs = stmt.executeQuery(query);
refer this
what I want to do :
input sql statement, and get the column name set . like this :
select email as "邮件" from t_user
2.what I hava done:
String url = "jdbc:mysql://127.0.0.1:3306/testcode?useUnicode=true&characterEncoding=utf8";
Statement st = conn.createStatement();
st.setFetchSize(1);
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData rmd = rs.getMetaData();
int columnCount = rmd.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String column = rmd.getColumnName(i);
}
what's problem I got:
the column name is garbage characters(except english letters),but the resultset is ok.
I am running an SQL query on a AS400 table.
I dont know in advance the columns names i am extracting in my SQL.
in my ResultSet i need to:
get the result set columns (MetaData of the result records - one time).
for each record on the set get the values of the columns.
How can i do this?
There are DataBaseMetaData retrieved from Connection.getMetaData() and ResultSetMetaData from a ResultSet.
Some example java code snipped that may help you:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM MY_TABLE" );
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
/*if you need the column names...*/
//for( int i = 1; i <= numberOfColumns ) {
// rsmd.getColumnName( i );
//}
List<Object[]> result = new ArrayList<Object[]>();
while( rs.next() ) {
Object[] values = new Object[ numberOfColumns ];
for( int index = 0; index < numberOfColumns; ) {
values[ index ] = rs.getObject( ++index );
}
result.add( values );
}