Olá pessoal,
Montei uma classe no java para conectar e pegar os dados de determinado comando SQL então até ai tudo bem…eu coloco um main na classe e chamo connecta e executasql e ele retorna os dados certo mas quando eu tenho usar esta classe no meu frame principal ele conecta,manda o SQL mas não pega os dados do Resultset e me retorna um erro:
run:
Exception in thread “main” java.lang.NullPointerException
at NewJPanel.main(NewJPanel.java:62)
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 10 segundos)
Codigo da classe do banco de dados
package utilitarios;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.*;
public class connect
{
public String host = "xxxx";
public String db = "java";
public String user = "java";
public String password = "xxxx";
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
public void conecta()
{
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex)
{
}
try {
conn = DriverManager.getConnection("jdbc:mysql://"+host+"/"+db+"?" + "user="+user+"&password="+password+"");
JOptionPane.showMessageDialog(null,"Conectado!");
} catch (SQLException ex) {
// handle any errors
JOptionPane.showMessageDialog(null,"SQLException: " + ex.getMessage());
JOptionPane.showMessageDialog(null,"SQLState: " + ex.getSQLState());
JOptionPane.showMessageDialog(null,"VendorError: " + ex.getErrorCode());
}
}
public void executasql(String command)
{
//Executando SQL
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(command);
if (stmt.execute(command)) {
rs = stmt.getResultSet();
}
/* while (rs.next()) {
System.out.println("Dono: "+rs.getString("dono"));
System.out.println("Nome: "+rs.getString("nome"));
System.out.println("Porta: "+rs.getString("porta"));
System.out.println("IP: "+rs.getString("ip"));
System.out.println("Slots: "+rs.getString("slots"));
System.out.println("Hostname: "+rs.getString("hostname"));
System.out.println("Plugins: "+rs.getString("plugins"));
System.out.println("Lanmode: "+rs.getString("lanmode"));
System.out.println("Autostart: "+rs.getString("autostart"));
System.out.println("====================================");
} */
}
catch (SQLException ex){
// handle any errors
JOptionPane.showMessageDialog(null,"SQLException: " + ex.getMessage());
JOptionPane.showMessageDialog(null,"SQLState: " + ex.getSQLState());
JOptionPane.showMessageDialog(null,"VendorError: " + ex.getErrorCode());
JOptionPane.showMessageDialog(null,"SQL: " + command);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { } // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { } // ignore
stmt = null;
}
}
}
}
Frame principal
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilitarios.connect;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJPanel.java
*
* Created on 28/05/2010, 21:04:48
*/
/**
*
* @author Rafael
*/
public class NewJPanel extends javax.swing.JPanel {
/** Creates new form NewJPanel */
public NewJPanel() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
public static void main(String[] args) {
connect conn = new connect();
conn.conecta();
conn.executasql("SELECT * FROM servers");
try {
while (conn.rs.next()) {
System.out.println("Dono: " + conn.rs.getString("dono"));
System.out.println("Nome: " + conn.rs.getString("nome"));
System.out.println("Porta: " + conn.rs.getString("porta"));
System.out.println("IP: " + conn.rs.getString("ip"));
System.out.println("Slots: " + conn.rs.getString("slots"));
System.out.println("Hostname: " + conn.rs.getString("hostname"));
System.out.println("Plugins: " + conn.rs.getString("plugins"));
System.out.println("Lanmode: " + conn.rs.getString("lanmode"));
System.out.println("Autostart: " + conn.rs.getString("autostart"));
System.out.println("====================================");
}
} catch (SQLException ex) {
Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}