java database operation problems

Category: Java SE
 
ljdtt
2010-04-10 09:30:23

Sponsored Links
This is a database search of a class, compile always said when api, then there is an exception when running out to the old, depressing death of me do not know what is wrong, this is a book code. ; trouble you help ..
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import ; java.util.Collection;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/ **
*
* @ author cf
* /
public class RetrieveObject {
private Connection ; connection = null;
private ResultSet rs = null;
private ResultSetMetaData ; rsmd = null;

public RetrieveObject () {

}

;
public Collection getTableCollection (String sqlStr) {
System.out.println ("a collection of query execution:" + sqlStr);
Collection collection = new Vector ();
connection = connectjdbc.connection;

try {
rs = connection.prepareStatement (sqlStr). executeQuery ();
rsmd = rs.getMetaData ();
while (rs.next ()) {
Vector vdata = new Vector ();
for (int i = 1; i <= rsmd.getColumnCount (); i + +) {
; vdata.addElement (rs.getObject (i));
;}
collection.add (vdata);
}
} catch (java.sql.SQLException sql) {

sql.printStackTrace ();
return null;
}
return collection;
}
public DefaultTableModel getTableModel (String [] name, String sqlStr) {
Vector vname = new Vector ();
for (int i = 0; i <name.length; i + +) {
vname.addElement (name [ i]);
}
DefaultTableModel tableModel = new DefaultTableModel (vname, 0);

connection = connectjdbc.connection;
try {
rs = connection . prepareStatement (sqlStr). executeQuery ();
rsmd = rs.getMetaData ();
while (rs.next ()) {
Vector vdata = new Vector ();
; for (int i = 1; i <= rsmd.getColumnCount () ; i + +) {
; vdata.addElement (rs.getObject (i));
;}
tableModel.addRow (vdata);
}
} catch (java . sql.SQLException sql) {
sql.printStackTrace ();

return null;
}
return tableModel;
}

public Vector getObjectRow (String sqlStr) {
; Vector vdata = new Vector ();

; connection = connectjdbc.connection;
try {
rs = connection.prepareStatement (sqlStr). executeQuery ();
rsmd = rs.getMetaData ();
; while (rs.next ()) {
for (int i = 1; i <= rsmd.getColumnCount (); i + +) {
vdata.addElement ((String) rs.getObject (i));
}
}
} catch (java.sql.SQLException sql) {
sql.printStackTrace ();
; return null;
}
return vdata;
}
}

Sponsored Links

lt_031110
2010-04-10 09:43:57
1 "compilation always said when api" is probably because you use Vector, now most of the way with ArrayList
2 getObjectRow (String sqlStr) in which you used a cast vdata.addElement ((String) rs.getObject (i));
; there might be wrong, or at your mistakes and come out
zhangm4669
2010-04-10 09:51:21
Which outdated ah? Which version you use the jdk?
I only know that Vector is no longer recommended for use.
wolfsing
2010-04-10 09:56:39
does not seem outdated ah! However, I recommend you put Vector plus generic comparison safer
MOTOR_SUI
2010-04-10 10:09:23
out what is wrong?
chenlei6189
2010-04-10 10:12:38
version is not the problem, oh? Some functions have been canceled, oh
zhzhyushan
2010-04-10 10:19:42
This I was not very clear ...... But there may be problems package! You can put the package name at
import java.sql. *;
Import java.util. *;
Import javax.swing. *;
Try
program would not be a necessary thing to try
Hey
kulabika
2010-04-10 10:29:52
bar error made up
juhuanjin
2010-04-10 10:41:49
first ensure that the database connection is successful now,
connecting sqlserver2000 Here is the sample code:
import java.sql.*;
public class Stu{
public static void main(String[] args){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
System.out.println("loading...");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String password="sa"; //sa
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println("sucessed...");
/*
Statement stmt=conn.createStatement();
String sql="select * from stu_inf";

ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
String sex=rs.getString("sex");
String add=rs.getString("address");
System.out.println(id+" "+name+" "+sex+" "+add);
}
*/
}catch(Exception e){
System.out.println("failed!");
e.printStackTrace();
}
}
}
hyhdsg888
2010-04-10 10:58:03
Now compile time without warning , I wonder to Caesar, but there are exceptions when running.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
; at data.RetrieveObject.getTableModel (RetrieveObject.java: 64)
at view.grade.initComponents (grade.java: 127)
at view.grade. <init> (grade.java: 29)
at view.appmain $ action.actionPerformed (appmain.java: 106)
at java.awt.MenuItem.processActionEvent (MenuItem.java: 627)
at java.awt.MenuItem.processEvent (MenuItem.java: 586)
at java.awt.MenuComponent.dispatchEventImpl (MenuComponent.java: 300)
at java.awt.MenuComponent.dispatchEvent (MenuComponent.java: 288)
at java.awt. EventQueue.dispatchEvent (EventQueue.java: 602)
at java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java: 269)
at java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java: 184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java: 174)
at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread. java: 169)
at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java: 161)
at java.awt.EventDispatchThread.run (EventDispatchThread.java: 122)
This is the form in which I made a piece of code:
DefaultTableModel tablemodel = null;
String [] name = { "Class ID", "year number", "class name"};
String sqlStr = " ; select * from class ";
data.RetrieveObject bdt = new data.RetrieveObject ();
tablemodel = bdt.getTableModel (name, sqlStr);
jTable1.setModel (tablemodel);
Please help me look.
catman2004
2010-04-10 11:14:51
64 yes phrase
Vector vname = new Vector ();
kle99
2010-04-10 11:30:51
Gloomy death of me, these three methods regardless of the call which will appear this exception java.lang.NullPointerException
Who can tell how it A ......... sentence has exceptions that even the
rs = connection.prepareStatement (sqlStr). executeQuery ();
have abnormal .. .
yuan0114
2010-04-10 11:45:15
DefaultTableModel tablemodel = null;

tablemodel = bdt.getTableModel (name, sqlStr);
these two places have issue, reported that a null pointer that you use a local variable, but the variable value null, LZ to single-step debugging to see if the variable is worth changing
extragalacti
2010-04-10 11:59:47
null pointer exception, will be able to step through found. Make a break with it.
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.