希望通过连接到java

时间:2016-02-09 21:04:53

标签: java mysql sql eclipse swing

首先,我想指出我是一个没有太多经验的新手程序员,所以请原谅我这个问题对某些人来说是否显而易见。此外,由于我不确定问题是什么,我会尝试提供尽可能多的信息,所以很抱歉我似乎在絮絮叨叨。无论如何,我要做的是创建一个带有Jframe的java应用程序,并让它与数据库进行交互。我对数据库或mysql没有任何经验,但我能够跟随youtube视频,并创建了一个成功连接到数据库,创建表并将信息插入该表的java项目。我想我可以在我的项目中使用与我的框架相同的连接功能。

然而,当我尝试这个时,程序不会在控制台中连接或显示任何内容,只需启动我的jframe即可。它似乎连接部分甚至没有?我使用的连接功能是:

public static Connection getConnection() throws Exception{
        try {String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "password";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,username,password);
        System.out.println("connected");
        return conn;
        }catch(Exception e){System.out.println(e);}

        return null;
    }    

这是我的完整代码,对不起,如果难以阅读,我在eclipse的窗口构建器中完成了大部分初始编码。获取连接和发布到数据库的功能位于主要之后的底部(将它们放在主要部分之前但移动它们希望它会产生影响)任何所有帮助都表示赞赏!

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
public class TemplatePractice extends JFrame {
private JTextField HTHoursWorked;
private JTextField HTHourlyRate;
private JTextField HTCCTips;
private JTextField PCHours;
private JTextField PCTotal;
private JTextField DisplayTP;
private JTextField DispGPPP;
private JTextField DispBnqtPay;
private JTextField DispEstPay;
JTextField DispActualPC;

private final Action action = new SwingAction();
private TemplatePractice() {
final JTextField PCTotal;


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridLayout(3, 1, 0, 0));

    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    JLabel lblNewLabel_1 = new JLabel("Date Worked");
    panel.add(lblNewLabel_1);

    JDateChooser PCDateWorked = new JDateChooser();
    panel.add(PCDateWorked);

    JLabel lblHoursWorked = new JLabel("Hours Worked:");
    panel.add(lblHoursWorked);

    HTHoursWorked = new JTextField();
    panel.add(HTHoursWorked);
    HTHoursWorked.setColumns(10);

    JRadioButton rdbtnRestaruant = new JRadioButton("Restaruant:");
    panel.add(rdbtnRestaruant);

    JRadioButton rdbtnBanquet = new JRadioButton("Banquet");
    panel.add(rdbtnBanquet);

    JLabel lblHoursWorked_1 = new JLabel("Hourly Rate: $");
    panel.add(lblHoursWorked_1);

    HTHourlyRate = new JTextField();
    panel.add(HTHourlyRate);
    HTHourlyRate.setColumns(10);

    JLabel lblCreditCardTips = new JLabel("Credit Card Tips:");
    panel.add(lblCreditCardTips);

    HTCCTips = new JTextField();
    panel.add(HTCCTips);
    HTCCTips.setColumns(10);

    JButton HTAddBtn = new JButton("Add");
    panel.add(HTAddBtn);

    JLabel label = new JLabel("");
    panel.add(label);

    JLabel label_1 = new JLabel("");
    panel.add(label_1);

    JPanel PCPanel = new JPanel();
    getContentPane().add(PCPanel);

    JLabel lblPayPeriodStart = new JLabel("Pay Period Start:");
    PCPanel.add(lblPayPeriodStart);

    JDateChooser PCStartDate = new JDateChooser();
    PCPanel.add(PCStartDate);

    JLabel lblPayPeriodEnd = new JLabel("Pay Period End");
    PCPanel.add(lblPayPeriodEnd);

    JDateChooser dateChooser_2 = new JDateChooser();
    PCPanel.add(dateChooser_2);

    JLabel lblTotalHours = new JLabel("Total Hours:");
    PCPanel.add(lblTotalHours);

    PCHours = new JTextField();
    PCPanel.add(PCHours);
    PCHours.setColumns(10);

    JLabel lblP = new JLabel("Paycheck Amount: $");
    PCPanel.add(lblP);

    PCTotal = new JTextField();
    PCPanel.add(PCTotal);
    PCTotal.setColumns(10);

    JButton PCAddBtn = new JButton("Add");
    PCAddBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double paydoub = Double.parseDouble(PCTotal.getText());
            int i;
            for (i=0;i<1000;i++){
                double paycheckdouble=0;
                paycheckdouble+=paydoub;

            double paycheckTotal=paycheckdouble;

            String paytotal= String.valueOf(paycheckTotal);

            DisplayTP.setText(paytotal);
            }
        }
    });
    PCAddBtn.setAction(action);
    PCPanel.add(PCAddBtn);



    JPanel panel_1 = new JPanel();
    getContentPane().add(panel_1);

    JButton btnCalculate = new JButton("Calculate");
    panel_1.add(btnCalculate);

    JLabel lblTotalPay = new JLabel("total pay:");
    panel_1.add(lblTotalPay);

    DisplayTP = new JTextField();
    panel_1.add(DisplayTP);
    DisplayTP.setColumns(10);

    JLabel lblGratPerParty = new JLabel("Grat Per Party");
    panel_1.add(lblGratPerParty);

    DispGPPP = new JTextField();
    panel_1.add(DispGPPP);
    DispGPPP.setColumns(10);

    JLabel lblBanquetPay = new JLabel("Banquet Pay");
    panel_1.add(lblBanquetPay);

    DispBnqtPay = new JTextField();
    panel_1.add(DispBnqtPay);
    DispBnqtPay.setColumns(10);

    DispEstPay = new JTextField("");
    panel_1.add(DispEstPay);
    DispEstPay.setColumns(10);

    DispActualPC = new JTextField();
    panel_1.add(DispActualPC);
    DispActualPC.setColumns(10);
}
    public static void main(String args[]) throws Exception{
        getConnection();
        post();
        TemplatePractice frame = new TemplatePractice();
        frame.setVisible(true);
        frame.setSize(600,400);
    }
    public static Connection getConnection() throws Exception{
        try {String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/test";
        String username = "root";
        String password = "password";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,username,password);
        System.out.println("connected");
        return conn;
        }catch(Exception e){System.out.println(e);}

        return null;
    }

    public static void post() throws Exception{
        final String var1 = "john";
        final String var2 = "smith";
        try{
        Connection Con = getConnection();
        PreparedStatement posted = Con.prepareStatement("INSERT INTO tablename(first,last) VALUES ('"+var1+"','"+var2+"')");
        posted.executeUpdate(); 
    }catch(Exception e){System.out.println(e);}
        finally {System.out.println("Insert Completed");}
    }




private class SwingAction extends AbstractAction {
    public SwingAction() {
        putValue(NAME, "SwingAction");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
    }
}

}

0 个答案:

没有答案