Seguinte:
Fiz esse codigo conforme vi num livro, mas não to conseguindo fazer a parte de banco de dados funcionar, o que devo fazer no banco de dados para que o mesmo funcione??/
Segue o codigo:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Container;
import java.net.URL;
import java.sql.*;
import javax.swing.*;
public class Leituraw{
public Leituraw(){
(new Cadastro()).show();
}
static boolean verificaWarning(SQLWarning w, String str) throws SQLException{
if(w != null){
System.out.println(str);
while (w != null) {
System.out.println("SQLState: " + w.getSQLState());
System.out.println("Mensagem: " + w.getMessage());
System.out.println("ErrorCode: " + w.getErrorCode() + " ");
w = w.getNextWarning();
}
return true;
}
return false;
}
private static void apresentaResultado(ResultSet rs) throws SQLException{
int i=0;
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
System.out.println(numCols);
// System.out.println(rsmd.getColumnLabel(i));
// System.out.println(", ");
while(rs.next()){
for (i=1; i<=numCols; i++){
System.out.println(" "+rsmd.getColumnName(i)+"= "+rs.getString(i));
}
System.out.println("");
}
}
private static void verificaExcecoes(SQLException e, String str){
System.out.println(str);
while(e != null){
System.out.println("SQLState: " + e.getSQLState());
System.out.println("Mensagem: " + e.getMessage());
System.out.println("ErrorCode: " + e.getErrorCode() + "");
e = e.getNextException();
}
}
public static void main (String[] args) {
String url = "jdbc:odbc:North";
String pergunta = "SELECT * FROM Relação";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection (url,"","");
verificaWarning(con.getWarnings(), "WARNING");
DatabaseMetaData dmd = con.getMetaData();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(pergunta);
Leituraw L = new Leituraw();
rs.close();
stmt.close();
con.close();
}
catch(SQLException e) {
verificaExcecoes(e, "Captura das SQLException");
}
catch(java.lang.Exception e){
e.printStackTrace();
}
}
private class Cadastro extends JFrame{
private JPanel lateral, base;
private JTextField jtext, jtext2, jtext3;
public Cadastro()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(150,200,280,200);
lateral = new JPanel();
SpringLayout layout = new SpringLayout();
JLabel label = new JLabel(" Nome:");
layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, lateral);
layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, lateral);
lateral.add(label);
lateral.setLayout(layout);
JTextField jtext = new JTextField(50);
layout.putConstraint(SpringLayout.WEST, jtext, 5, SpringLayout.EAST, label);
layout.putConstraint(SpringLayout.NORTH, jtext, 5, SpringLayout.NORTH, lateral);
lateral.add(jtext);
lateral.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(lateral, BorderLayout.CENTER);
base = new JPanel ();
base.setLayout(new FlowLayout());
base.add(new JButton("Cadastrar"));
base.add(new JButton("Cancelar"));
base.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(base, BorderLayout.SOUTH);
}
private class TextFieldHandler implements ActionListener{
public void actionPerformed( ActionEvent e )
{
String s = "";
if(e.getSource() == jtext)
s = "text1: " + e.getActionCommand();
else if(e.getSource() == jtext2)
s = "text2: " + e.getActionCommand();
else if(e.getSource() == jtext3)
s = "text3: " + e.getActionCommand();
JOptionPane.showMessageDialog (null, s);
}
}
}
}