访问被拒绝Netbeans数据库到MySQL

时间:2015-12-04 22:36:48

标签: java mysql database netbeans

我正在创建一个GUI,您可以输入信息示例Type,Driver和Id 我正在尝试将信息添加到我在MySQL中称为Vehicle的数据库中。每次我运行它并单击添加信息它会给我一个拒绝访问的消息。我没有密码。

 package databaseprojectgui;

import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.*;

public class DatabaseprojectGui extends JFrame{
JLabel DriverLabel,TypeLabel,IdLebel;
 JTextField DriverText,TypeText,IdText;
JButton btn_insert,btn_update,btn_delete;
public DatabaseprojectGui(){
super("INSERT AND CALL DATA");
DriverLabel = new JLabel("Driver :");
TypeLabel = new JLabel("Type:");
IdLebel = new JLabel("Id:");
DriverLabel.setBounds(20, 50, 100, 20);
 TypeLabel.setBounds(20, 80, 100, 20);
IdLebel.setBounds(20, 110, 100, 20);

DriverText = new JTextField(20);
TypeText = new JTextField(20);
IdText = new JTextField(20);
DriverText.setBounds(130, 50, 150, 20);
 TypeText.setBounds(130, 80, 150, 20);
IdText.setBounds(130, 110, 150, 20);
btn_insert = new JButton("Insert");
btn_update = new JButton("Results");
btn_insert.setBounds(300, 50, 80, 20);
btn_update.setBounds(300, 80, 80, 20);


setLayout(null);
add(DriverLabel);
add(TypeLabel);
add(IdLebel);
add(DriverText);
add(TypeText);
add(IdText);
add(btn_insert);
add(btn_update);


//button insert
 btn_insert.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
 try{
  theQuery("insert into Food (foodEntree,foodSide,foodPrice)        values('"+DriverText.getText()+"','"+TypeText.getText()+"',"+IdText.getText()+")"    );
}
catch(Exception ex){}
}
});

//button result
btn_update.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
try{
 Class.forName("com.mysql.jdbc.Driver");
 Connection con =      DriverManager.getConnection("jdbc:mysql://localhost/Vehicle","root","");
Statement select = con.createStatement();
ResultSet result = select
.executeQuery("SELECT Driver, Type, Id FROM Car");

 StringBuilder str = new StringBuilder();

while (result.next())
 {
  str.append("Driver: " + result.getString("Driver"));
 str.append("---");
 str.append("Type: " + result.getString("Type"));
 str.append("---");
 str.append("Id:" + result.getString("Id"));

 str.append("\n");



 }
 JOptionPane.showMessageDialog(null,str.toString());

 }
 catch(Exception ex){}
}
 });



 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 setVisible(true);
 setLocationRelativeTo(null);
 setSize(500,200);

}

 //function to execute the insert update delete query
      public void theQuery(String query){
   Connection con = null;
   Statement st = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");
        con =          DriverManager.getConnection("jdbc:mysql://localhost/Vehicle","root","");
   st = con.createStatement();
st.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Query Executed");
}catch(Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}


public static void main(String[] args){

new DatabaseprojectGui();
}
}

0 个答案:

没有答案