JCW2
asked on
Mysql Problems
I'm working with a Netbeans project.
Part 1: in this project, how do I refer the string in String url = "jdbc:mysql://localhost:33 06/univers ity"; to another database?
Part 2: how do I load an sql file in mysl (command line)
Link for the 7z file: (Change the file extension from zip to 7z)
http://www.zipgenius.com/i ndex.php?i d=33
ExpertsExchange.zip
Part 1: in this project, how do I refer the string in String url = "jdbc:mysql://localhost:33
Part 2: how do I load an sql file in mysl (command line)
Link for the 7z file: (Change the file extension from zip to 7z)
http://www.zipgenius.com/i
ExpertsExchange.zip
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have my Netbeans project and mysql-connector-java-5.1.1 5.zip in the archive. The second file has a jar file the project needs to load.
ASKER
MySQL download URL:
http://www.mysql.com/downl oads/
http://www.mysql.com/downl
// Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Rajan Alex
*/
import TableModel.DisplayResult;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new DisplayResult();
}
}
// DisplayResult.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package TableModel;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.table.*;
/**
*
* @author Rajan Alex
*/
public class DisplayResult extends JFrame {
String url = "jdbc:mysql://localhost:3306/university";
String username = "root";
String password = "194157";
static final String defaultQuery = "select * from instructor";
private ResultSetTableModel tableModel;
private JTextArea queryArea;
public DisplayResult() {
super("Displaying Query Result");
try {
tableModel = new ResultSetTableModel(url, username, password, defaultQuery);
queryArea = new JTextArea("Enter your query here", 3, 100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);
JScrollPane scrollPane =
new JScrollPane(queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JButton submitButton = new JButton("Submit Query");
Box northBox = Box.createHorizontalBox();
northBox.add(scrollPane);
northBox.add(submitButton);
JTable resultTable = new JTable(tableModel);
add(northBox, BorderLayout.NORTH);
add(new JScrollPane(resultTable), BorderLayout.CENTER);
submitButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tableModel.setQuery(queryArea.getText());
} catch (SQLException sqle) {
JOptionPane.showMessageDialog(null,
sqle.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE);
try {
tableModel.setQuery(defaultQuery);
queryArea.setText(defaultQuery);
} catch (SQLException sqle1) {
JOptionPane.showMessageDialog(null,
sqle1.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(1);
}
}
} //end method
});
final TableRowSorter<TableModel> sorter =
new TableRowSorter<TableModel>(tableModel);
resultTable.setRowSorter(sorter);
setSize(500, 250);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent we) {
tableModel.disconnectFromDatabase();
System.exit(0);
}
});
} catch (SQLException sqle) {
JOptionPane.showMessageDialog(null,
sqle.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE);
tableModel.disconnectFromDatabase();
System.exit(1);
}
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
// ResultSetTableModel.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package TableModel;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
/**
*
* @author Rajan Alex
*/
public class ResultSetTableModel extends AbstractTableModel {
// experts exchange
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private boolean connectedToDatabase;
private int numberOfRows;
public ResultSetTableModel(String url, String userName, String password,
String query) throws SQLException {
connection = DriverManager.getConnection(url, userName, password);
statement = connection.createStatement(); // i am not sure of parameters
connectedToDatabase = true;
setQuery(query);
}
public Class getColumnClass(int c) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
try {
String className = metaData.getColumnClassName(+1);
return Class.forName(className);
} catch (Exception e) {
e.printStackTrace();
}
return Object.class; // to ingore while compiling
}
public int getColumnCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
try {
return metaData.getColumnCount();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return 0; // if problems occur above
}
public String getColumnName(int c) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
try {
return metaData.getColumnName(c + 1);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return ""; // if problem occur above
}
public int getRowCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
return numberOfRows;
}
public Object getValueAt(int r, int c) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
try {
resultSet.absolute(r + 1);
return resultSet.getObject(c + 1);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
return ""; // if problem occur above
}
public void setQuery(String st) throws SQLException, IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not connected to database");
}
resultSet = statement.executeQuery(st);
metaData = resultSet.getMetaData();
resultSet.last();
numberOfRows = resultSet.getRow();
fireTableStructureChanged();
}
public void disconnectFromDatabase() {
if (connectedToDatabase) {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
connectedToDatabase = false;
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm back; with regards to the database in the code; how do I change it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your help.
ASKER