程序打完了,又改了好久
运行了下发现出现下面的异常
Exception in thread "main" java.lang.OutofMemoryError: Java heap space
希望那位前辈能copy到自己机子上运行下
告诉我应该 改那些地方
谢谢了!
代码如下:
—————————————————————————————————
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class QueryDB
{
public static void main(String[] args)
{
JFrame frame = new QueryDBFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class QueryDBFrame extends JFrame
{
public QueryDBFrame()
{
setTitle("QueryDB");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
authors = new JComboBox();
authors.setEditable(false);
authors.addItem("Any");
publishers = new JComboBox();
publishers.setEditable(false);
publishers.addItem("Any");
result = new JTextArea(4,50);
result.setEditable(false);
priceChange = new JTextField(8);
priceChange.setText("-5.00");
try
{
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "xionggang";
String dbName = "xionggangdb";
String url = "jdbc:mysql://lacolhost//"+dbName;
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,userName,passWord);
Statement stat = conn.createStatement();
String query = "SELECT Name FROM Authors";
ResultSet rs = stat.executeQuery(query);
while(rs.next())
{
authors.addItem(rs.getString(1));
}
rs.close();
query = "SELECT Name FROM Publishers";
rs = stat.executeQuery(query);
while(rs.next())
{
publishers.addItem(rs.getString(1));
}
rs.close();
stat.close();
}
catch(SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
}
}
catch(Exception e)
{
result.setText(""+e);
}
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
gridbag.setConstraints(authors,c);
add(authors);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(publishers,c); add(publishers);
JButton queryButton = new JButton("Query");
queryButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
executeQuery();
}
});
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(queryButton,c);
add(queryButton);
JButton changeButton =new JButton("change price");
changeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
changePrices();
}
});
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(changeButton,c);
add(changeButton);
c.weightx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(priceChange,c);
add(priceChange);
c.gridwidth = 1;
c.gridheight = 2;
c.weighty = 1.0;
JScrollPane jsc = new JScrollPane(result);
c.weightx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(jsc,c);
add(jsc);//,new GBC(0,2,4,1).setFill(GBC.BOTH).setWeight(100,100));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
try
{
if(conn!=null)
conn.close();
}
catch(SQLException e)
{
while(e!=null)
{
e.printStackTrace();
e = e.getNextException();
}
}
}
});
}
private void executeQuery()
{
ResultSet rs = null;
try
{
String author = (String)authors.getSelectedItem();
String publisher = (String)publishers.getSelectedItem();
if(!author.equals("Any")&&!publisher.equals("Any"))
{
if(authorPublisherQueryStmt == null)
authorPublisherQueryStmt.setString(1,author);
authorPublisherQueryStmt.setString(2,publisher);
rs = authorPublisherQueryStmt.executeQuery();
}
else if(!author.equals("Any")&&publisher.equals("Any"))
{
if(authorQueryStmt == null)
authorQueryStmt = conn.prepareStatement(authorQuery);
authorQueryStmt.setString(1,author);
rs = authorQueryStmt.executeQuery();
}
else if (author.equals("Any")&&!publisher.equals("Any"))
{
if(publisherQueryStmt ==null)
publisherQueryStmt = conn.prepareStatement(publisherQuery);
publisherQueryStmt.setString(1,publisher);
rs = publisherQueryStmt.executeQuery();
}
else
{
if(allQueryStmt ==null)
allQueryStmt = conn.prepareStatement(allQuery);
rs = allQueryStmt.executeQuery();
}
result.setText("");
while(rs.next())
{
result.append(rs.getString(1));
result.append(",");
result.append(rs.getString(2));
result.append("\n");
}
rs.close();
}
catch(SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
e=e.getNextException();
}
}
}
public void changePrices()
{
String publisher = (String)publishers.getSelectedItem();
if (publisher.equals("Any"))
{
result.setText("Iam sorry,but I can not do that");
}
try
{
if(priceUpdateStmt == null)
priceUpdateStmt = conn.prepareStatement(priceUpdate);
priceUpdateStmt.setString(1,priceChange.getText());
priceUpdateStmt.setString(2,publisher);
int r =priceUpdateStmt.executeUpdate();
result.setText(r+"records updated.");
}
catch (SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
e = e.getNextException();
}
}
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
private JComboBox authors;
private JComboBox publishers;
private JTextField priceChange;
private JTextArea result;
private Connection conn;
private PreparedStatement authorQueryStmt;
private PreparedStatement authorPublisherQueryStmt;
private PreparedStatement publisherQueryStmt;
private PreparedStatement allQueryStmt;
private PreparedStatement priceUpdateStmt;
private static final String authorPublisherQuery =
"select Books.price,Books.Title from Books,BooksAuthors,Authors,Publishers"+
"where Authors.Author_Id = BookAuthors.Author_Id AND BooksAuthor.ISBN = Books.ISBN"+
"AND Books Publisher_Id = Publisher.Publisher_Id AND Authors.Name = ?"+
"AND Publisher.Name = ?";
private static final String authorQuery =
"select Books.Price,Books.Title from Books,BookAuthors,Authors"+
"where Authors.Author_Id = BooksAuthor_Id AND BooksAuthors.ISBN = Book.ISBN"+
"AND Author.Name = ?";
private static final String publisherQuery =
"select Books.Price,Books.Title from Books,Publishers"+
"where Books.Publisher_Id = Publishers.Publisher_Id AND Publisher.Name = ?";
private static final String allQuery = "select Books.Price,Books.Title from Books";
private static final String priceUpdate =
"update Books" +"SET Price = Price +?" +
"where Books.Publisher_Id = (select Publisher_Id from Publishers where Name = ?";
}
运行了下发现出现下面的异常
Exception in thread "main" java.lang.OutofMemoryError: Java heap space
希望那位前辈能copy到自己机子上运行下
告诉我应该 改那些地方
谢谢了!
代码如下:
—————————————————————————————————
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class QueryDB
{
public static void main(String[] args)
{
JFrame frame = new QueryDBFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class QueryDBFrame extends JFrame
{
public QueryDBFrame()
{
setTitle("QueryDB");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gridbag);
authors = new JComboBox();
authors.setEditable(false);
authors.addItem("Any");
publishers = new JComboBox();
publishers.setEditable(false);
publishers.addItem("Any");
result = new JTextArea(4,50);
result.setEditable(false);
priceChange = new JTextField(8);
priceChange.setText("-5.00");
try
{
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "xionggang";
String dbName = "xionggangdb";
String url = "jdbc:mysql://lacolhost//"+dbName;
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,userName,passWord);
Statement stat = conn.createStatement();
String query = "SELECT Name FROM Authors";
ResultSet rs = stat.executeQuery(query);
while(rs.next())
{
authors.addItem(rs.getString(1));
}
rs.close();
query = "SELECT Name FROM Publishers";
rs = stat.executeQuery(query);
while(rs.next())
{
publishers.addItem(rs.getString(1));
}
rs.close();
stat.close();
}
catch(SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
}
}
catch(Exception e)
{
result.setText(""+e);
}
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
gridbag.setConstraints(authors,c);
add(authors);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(publishers,c); add(publishers);
JButton queryButton = new JButton("Query");
queryButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
executeQuery();
}
});
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(queryButton,c);
add(queryButton);
JButton changeButton =new JButton("change price");
changeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
changePrices();
}
});
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(changeButton,c);
add(changeButton);
c.weightx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(priceChange,c);
add(priceChange);
c.gridwidth = 1;
c.gridheight = 2;
c.weighty = 1.0;
JScrollPane jsc = new JScrollPane(result);
c.weightx = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(jsc,c);
add(jsc);//,new GBC(0,2,4,1).setFill(GBC.BOTH).setWeight(100,100));
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
try
{
if(conn!=null)
conn.close();
}
catch(SQLException e)
{
while(e!=null)
{
e.printStackTrace();
e = e.getNextException();
}
}
}
});
}
private void executeQuery()
{
ResultSet rs = null;
try
{
String author = (String)authors.getSelectedItem();
String publisher = (String)publishers.getSelectedItem();
if(!author.equals("Any")&&!publisher.equals("Any"))
{
if(authorPublisherQueryStmt == null)
authorPublisherQueryStmt.setString(1,author);
authorPublisherQueryStmt.setString(2,publisher);
rs = authorPublisherQueryStmt.executeQuery();
}
else if(!author.equals("Any")&&publisher.equals("Any"))
{
if(authorQueryStmt == null)
authorQueryStmt = conn.prepareStatement(authorQuery);
authorQueryStmt.setString(1,author);
rs = authorQueryStmt.executeQuery();
}
else if (author.equals("Any")&&!publisher.equals("Any"))
{
if(publisherQueryStmt ==null)
publisherQueryStmt = conn.prepareStatement(publisherQuery);
publisherQueryStmt.setString(1,publisher);
rs = publisherQueryStmt.executeQuery();
}
else
{
if(allQueryStmt ==null)
allQueryStmt = conn.prepareStatement(allQuery);
rs = allQueryStmt.executeQuery();
}
result.setText("");
while(rs.next())
{
result.append(rs.getString(1));
result.append(",");
result.append(rs.getString(2));
result.append("\n");
}
rs.close();
}
catch(SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
e=e.getNextException();
}
}
}
public void changePrices()
{
String publisher = (String)publishers.getSelectedItem();
if (publisher.equals("Any"))
{
result.setText("Iam sorry,but I can not do that");
}
try
{
if(priceUpdateStmt == null)
priceUpdateStmt = conn.prepareStatement(priceUpdate);
priceUpdateStmt.setString(1,priceChange.getText());
priceUpdateStmt.setString(2,publisher);
int r =priceUpdateStmt.executeUpdate();
result.setText(r+"records updated.");
}
catch (SQLException e)
{
result.setText("");
while(e!=null)
{
result.append(""+e);
e = e.getNextException();
}
}
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 400;
private JComboBox authors;
private JComboBox publishers;
private JTextField priceChange;
private JTextArea result;
private Connection conn;
private PreparedStatement authorQueryStmt;
private PreparedStatement authorPublisherQueryStmt;
private PreparedStatement publisherQueryStmt;
private PreparedStatement allQueryStmt;
private PreparedStatement priceUpdateStmt;
private static final String authorPublisherQuery =
"select Books.price,Books.Title from Books,BooksAuthors,Authors,Publishers"+
"where Authors.Author_Id = BookAuthors.Author_Id AND BooksAuthor.ISBN = Books.ISBN"+
"AND Books Publisher_Id = Publisher.Publisher_Id AND Authors.Name = ?"+
"AND Publisher.Name = ?";
private static final String authorQuery =
"select Books.Price,Books.Title from Books,BookAuthors,Authors"+
"where Authors.Author_Id = BooksAuthor_Id AND BooksAuthors.ISBN = Book.ISBN"+
"AND Author.Name = ?";
private static final String publisherQuery =
"select Books.Price,Books.Title from Books,Publishers"+
"where Books.Publisher_Id = Publishers.Publisher_Id AND Publisher.Name = ?";
private static final String allQuery = "select Books.Price,Books.Title from Books";
private static final String priceUpdate =
"update Books" +"SET Price = Price +?" +
"where Books.Publisher_Id = (select Publisher_Id from Publishers where Name = ?";
}
是我的程序有些错了,那URL写错了
那循环有问题
还有那executeQuery()里面的语句也有问题。
我现在改好了