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.
Related
I have been developing small program in java, i made jtable and connect it to mysql, but it only shows 4 rows only no matter how big my data is...here is jtable in java
and here is mysql table data..
con = DBconnect.connect();
String[] columnNames = {"Name", "UserName", "UserType"};
model.setColumnIdentifiers(columnNames);
jTable1.setModel(model);
jTable1.setShowGrid(false);
try {
PreparedStatement pst;
pst = con.prepareStatement("select * from courses");
ResultSet rs = pst.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
int i = 0;
for(i = 0; i <= columns; i++){
if (rs.next()) {
uname = rs.getString("C_ID");
email = rs.getString("C_NAME");
pass = rs.getString("C_TYPE");
model.addRow(new Object[]{uname, email, pass});
}
}
You are mixing columns and rows, for(i = 0; i <= columns; i++) will only read 4 records/rows (because you have 3 columns) .
Simply browse the resultset with while(rs.next()) to get all the data :
while(rs.next()){
uname = rs.getString("C_ID");
email = rs.getString("C_NAME");
pass = rs.getString("C_TYPE");
model.addRow(new Object[]{uname, email, pass});
}
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
Part of my code looks like this:
int size = 0;
try {
String query = "SELECT count(*) FROM users";
statement = connection.prepareStatement(query);
statement.execute(query);
I don't know what to do to set size of table to my int size.
The result set will contain the value of numner of rows. get the value into your size variable.
ResultSet result = statement.executeQuery(query);
while (result.next()){
size= result.getInt(1);
}
System.out.println("Number of row:"+size);
int size = 0;
try {
String query = "SELECT count(*) FROM users";
statement = connection.prepareStatement(query);
ResultSet rs = statement.execute(query);
size = rs.getInt(1);
rs.getInt(1) will return the first row from the query as int
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 );
}
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.