Estou tentando fazer uma rotina de inclusão…Tenho uma interface gráfica para isso…quando executo e clico no botão inserir aparece o seguinte erro:
Já configurei o classpath setei para os tres arquivos .jar.
java.lang.ClassNotFoundException:
com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at Conexao.Conecta.getConnection(Conecta.java:77)
at janCategoria.cmdInserirActionPerformed(janCategoria.java:136)
at janCategoria.access$100(janCategoria.java:11)
at janCategoria$2.actionPerformed(janCategoria.java:83)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
-----------código-----------------
CLASSE: CONEXAO
package Conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conecta {
private Connection con = null;
private String hostName = null;
private String userName = null;
private String password = null;
private String url = null;
private String jdbcDriver = null;
private String dataBaseName = null;
private String dataBasePrefix = null;
private String dabaBasePort = null;
public Conecta() {
super();
hostName = "suport03";
userName = "user";
password = "123456";
jdbcDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
dataBaseName = "Midia";
dataBasePrefix = "jdbc:microsoft:sqlserver://";
dabaBasePort = "1433";
url = dataBasePrefix + hostName + ":"+dabaBasePort+"/" + dataBaseName;
}
/**
- Retorna uma java.sql.Connection.
-
@return con
/
public Connection getConnection() {
try {
if (con == null) {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(url, userName, password);
} else if (con.isClosed()) {
con = null;
return getConnection();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/* - Fecha a conexão com BD.
*/
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
CLASSE: janCategoria
public class janCategoria extends javax.swing.JFrame {
public janCategoria() {
initComponents();
}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
lblNome = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
tblDescricao = new javax.swing.JTable();
cmdInserir = new javax.swing.JButton();
cmdAlterar = new javax.swing.JButton();
cmdExcluir = new javax.swing.JButton();
cmdSair = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.GridBagLayout());
setTitle("Cadastro de Categoria");
setName("fraCategoria");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
lblNome.setText("Nome:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.ipadx = 4;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(lblNome, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 8;
gridBagConstraints.gridheight = 2;
gridBagConstraints.ipadx = 254;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(txtNome, gridBagConstraints);
tblDescricao.setFont(new java.awt.Font("Tahoma", 0, 10));
tblDescricao.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.gridwidth = 10;
gridBagConstraints.ipadx = 240;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(tblDescricao, gridBagConstraints);
cmdInserir.setText("Inserir");
cmdInserir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdInserirActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.ipadx = 5;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(cmdInserir, gridBagConstraints);
cmdAlterar.setText("Alterar");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 5;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(cmdAlterar, gridBagConstraints);
cmdExcluir.setText("Excluir");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 6;
gridBagConstraints.gridy = 5;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(cmdExcluir, gridBagConstraints);
cmdSair.setText("Sair");
cmdSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdSairActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 8;
gridBagConstraints.gridy = 5;
gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
getContentPane().add(cmdSair, gridBagConstraints);
pack();
}
private void cmdInserirActionPerformed(java.awt.event.ActionEvent evt) {
Conecta conecta = new Conecta();
Statement stmt = null;
String descricao = txtNome.getText();
String codigo = "1";
String inserir = "insert into Categoria(codigo,descricao) values (codigo, descricao)";
try{
stmt = conecta.getConnection().createStatement();
stmt.executeUpdate(inserir);
stmt.close();
conecta.closeConnection();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Erro ao executar o Insert ou ao fechar a conexão");
}
}
private void cmdSairActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new janCategoria().show();
}
private javax.swing.JButton cmdAlterar;
private javax.swing.JButton cmdExcluir;
private javax.swing.JButton cmdInserir;
private javax.swing.JButton cmdSair;
private javax.swing.JLabel lblNome;
private javax.swing.JTable tblDescricao;
private javax.swing.JTextField txtNome;
}