Tenho 3 classes:
Classe Connection, classe JFrame e classe Research
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package research;
import java.sql.*;
import javax.swing.JOptionPane;
/**
*
* @author André Vinícius Lopes
*/
public class Connection {
public static void main ( String args[])
{
//PlayersDataBase.getConexaoMySQL();
//PlayersDataBase.statusConection();
//PlayersDataBase.SelectTest();
try
{
Class.forName("com.mysql.jdbc.Driver");
String serverName = "localhost"; //caminho do servidor do BD
String mydatabase = "MySQL"; //nome do seu banco de dados
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root"; //nome de um usuário de seu BD
String password = "root"; //sua senha de acesso
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
Statement stm = conn.createStatement();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
private Statement createStatement() {
throw new UnsupportedOperationException("Not yet implemented");
}
public static void insert(String x)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String serverName = "localhost"; //caminho do servidor do BD
String mydatabase = "mysql"; //nome do seu banco de dados
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root"; //nome de um usuário de seu BD
String password = "root"; //sua senha de acesso
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(x);
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public static void select(String y)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String serverName = "localhost"; //caminho do servidor do BD
String mydatabase = "mysql"; //nome do seu banco de dados
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root"; //nome de um usuário de seu BD
String password = "root"; //sua senha de acesso
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(y);
while (rs.next())
{
double virus = rs.getDouble("answer");
double id = rs.getInt("idvolunteers");
double reality = 0;
reality = (id / virus);
JOptionPane.showMessageDialog(null,reality + " " + " persons per virus ");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JFrame.java
*
* Created on 16/12/2011, 10:26:25
*/
package research;
/**
*
* @author André Vinícius Lopes
*/
public class JFrame extends javax.swing.JFrame {
/** Creates new form JFrame */
public JFrame() {
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() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(0, 0, 0));
jLabel1.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("=UPEO= Researchs ");
jButton1.setText("First Question");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Results Of First Question");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel2.setText("Made by Lopes and Weiner, all rights are left!");
jLabel3.setText("Beta Version 1.0a");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jLabel1)
.addGap(66, 66, 66)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 108, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(31, 31, 31))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 156, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Research.FirstQuestion();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
Research.ResultsOfFirstQuestion();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* Program that do Research and Take Values to my On-line DataBase
*
*
*/
package research;
import java.sql.*;
import javax.swing.JOptionPane;
/**
*
* @author André Vinícius Lopes
*/
public class Research {
public static int answer;
public static double Volunteers;
public static double answersOne;
public static double results;
public Research() {
}
public static void main(String[] args) {
JFrame.main(args);
Connection.main(args);
}
public static void FirstQuestion()
{
String aux = JOptionPane.showInputDialog(null,"How many Virus You Got This Month ?");
//Transforma o valor de aux em Int
answer = Integer.parseInt(aux);
//A resposta deve ir para o banco de dados
String x = "Insert into dba values ( 1 ,1, '" +answer+ "')";
Connection.insert(x);
}
public static void ResultsOfFirstQuestion()
{
//Número de pessoas:
//Volunteers
//A resposta deve VIR do banco de dados
//Número de virus:
//answersOne
//A resposta deve Vir Do banco de dados
//Resultados da média
String y = "Select * from dba ";
Connection.select(y);
}
}
Está causando erro com.mysql.jdbc.JDBC4Connection cannot be cast to research.Connection
antes disso, estava dando outro erro
Communications link failure Last packet sent to the server was 0 ms ago.
Voces podem me ajudar?
Estou usando mysql + netbeans