创建数据库需要帮助(Java)

创建数据库需要帮助(Java),java,database,arraylist,Java,Database,Arraylist,正如标题所示,我需要帮助来创建数据库。什么样的数据库不重要,我只需要它工作 该程序是一个“musiclist”,您可以添加歌曲、删除歌曲和编辑歌曲 我已经编写了大约一个月的程序,如果您也能解释一下数据库方法是如何工作的等,我将不胜感激,而不是仅仅给我一个答案:-) 数据库需要存储3个ArrayList的数据。3个ArrayList都存储字符串,我使用JList(Model)的索引连接ArrayList 我需要的功能是: 将3个ArrayList存储到数据库中。可能是因为ArrayList与索引

正如标题所示,我需要帮助来创建数据库。什么样的数据库不重要,我只需要它工作

该程序是一个“musiclist”,您可以添加歌曲、删除歌曲和编辑歌曲

我已经编写了大约一个月的程序,如果您也能解释一下数据库方法是如何工作的等,我将不胜感激,而不是仅仅给我一个答案:-)

数据库需要存储3个ArrayList的数据。3个ArrayList都存储字符串,我使用JList(Model)的索引连接ArrayList

我需要的功能是:

  • 将3个ArrayList存储到数据库中。可能是因为ArrayList与索引(如song(索引1)、Artister(索引1))连接在一起需要存储在一起
  • 我需要能够从数据库中删除一个特定的索引
  • 我需要能够从数据库中编辑,在一个特定的索引。我需要能够从单独的arraylist调用内容,例如:从歌曲中获取内容(索引5),所以将所有3个arraylist添加到1个字符串不是一个选项
我不确定使用3个ArrayList来存储数据是不是一个好主意,所以如果有更好的选择,我很乐意知道:-)

当我从数据库调用数据时,数据应该被调用到addString()方法。此方法获取3个字符串,并将它们添加到同一索引中。所以我想数据库不应该包含任何类型的索引

“edit”方法可在editSong()中找到;,“remove”方法位于ite.addActionListener中

3数组为:fl,包含歌曲名称;艺术,包含艺术家名称;youurl,其中包含youtube url

代码如下:

公共类MusicList扩展JFrame{

public JTextField af;
private JList jl;
private JButton add;
private JButton edit;
private JButton test;
private JPanel jp;
private JScrollPane sp;
private JTextField artist;
private JButton save;
private JButton listb;
private JPopupMenu jpo;
private JMenuItem ite;
private JButton editsave;
private JButton editlist;
private JTextField youtube;
private JLabel ytl;
private JLabel arti;
private JLabel songg;
int g;
//creates a DefaultListModel.. actions at the JList can be made through here. e.g if adding to jlist is m.addElement();
DefaultListModel<String> m = new DefaultListModel<String>();
//creates arraylists
List<String> fl = new ArrayList<String>();
List<String> art = new ArrayList<String>();
List<String> youurl = new ArrayList<String>();

public MusicList(){
    super("Musiclist - Alpha");
    setLayout(null);

    jl = new JList(m);
    add(jl);
    //creates a scrollpane, "implements jlist"
    sp = new JScrollPane(jl);
    sp.setBounds(30,30,195,200);
    add(sp);
    //creates the textfield to contain songname
    af = new JTextField(12);
    String afs = af.getText();
    af.setBounds(20,30,210,20);
    add(af);
    af.setVisible(false);
    //opens the add menu
    add = new JButton("add song");
    add.setBounds(20,250,100,20);
    add(add);
    //opens the edit menu
    edit = new JButton("edit song");
    edit.setBounds(135,250,100,20);
    add(edit);
    //this button checks if art and fl(arraylists) match to the index.
    test = new JButton("test");
    test.setBounds(300, 40, 80, 20);
    add(test);
    //the textfield which will pass the artist string.. used in add and edit
    artist = new JTextField();
    artist.setBounds(20,70,210,20);
    add(artist);
    artist.setVisible(false);
    //adds back button in "add" menu
    listb = new JButton("back");
    listb.setBounds(135,250,100,20);
    add(listb);
    listb.setVisible(false);
    //adds save button on "add" menu
    save = new JButton("save");
    save.setBounds(20,250,100,20);
    add(save);
    save.setVisible(false);
    //adds the back button on "edit" menu
    editlist = new JButton("back");
    editlist.setBounds(135, 250, 100, 20);
    add(editlist);
    editlist.setVisible(false);
    //adds the save button on "edit" menu
    editsave = new JButton ("save");
    editsave.setBounds(20,250,100,20);
    add(editsave);
    editsave.setVisible(false);
    //adds the youtube textfield
    youtube = new JTextField();
    youtube.setBounds(20,110,120,20);
    add(youtube);
    youtube.setVisible(false);
    //adds jlabel
    ytl = new JLabel("https://www.youtube.com/watch");
    ytl.setBounds(20,90,200,20);
    add(ytl);
    ytl.setVisible(false);

    arti = new JLabel("Artist");
    arti.setBounds(20,50,170,20);
    add(arti);
    arti.setVisible(false);

    songg = new JLabel("Song");
    songg.setBounds(20,10,170,20);
    add(songg);
    songg.setVisible(false);
    //button to open the add window
    add.addActionListener(
            new ActionListener(){
            public void actionPerformed(ActionEvent event){

                jl.setVisible(false);
                sp.setVisible(false);
                add.setVisible(false);
                edit.setVisible(false);
                listb.setVisible(true);
                save.setVisible(true);
                af.setVisible(true);    
                artist.setVisible(true);
                youtube.setVisible(true);
                ytl.setVisible(true);
                songg.setVisible(true);
                arti.setVisible(true);
                af.requestFocus();
                }});


    edit.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){ 

                    System.out.println(jl.getSelectedIndex());
                    //checks if theres an selected index.. unselected index = -1.
                    if(jl.getSelectedIndex()<0){
                        JOptionPane.showMessageDialog(null, "Please select a song to edit");
                    }else{
                    //open edit window
                    jl.setVisible(false);
                    sp.setVisible(false);
                    add.setVisible(false);
                    edit.setVisible(false);
                    editlist.setVisible(true);
                    editsave.setVisible(true);
                    af.setVisible(true);    
                    artist.setVisible(true);
                    youtube.setVisible(true);
                    ytl.setVisible(true);
                    songg.setVisible(true);
                    arti.setVisible(true);
                    //takes selected index, and set text of textfield af and artists to selected index.
                    final int i = jl.getSelectedIndex();
                    if(i>=0){
                    System.out.println(i);
                    af.setText(fl.get(i));
                    artist.setText(art.get(i));
                    youtube.setText(youurl.get(i));
                    }}}});
    //test button.. checks if index + song + artist match.
    test.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){

                    final int i = jl.getSelectedIndex();
                    if(i>=0){
                        //String j = (m.getElementAt(i));
                        //System.out.println(j);
                        System.out.println(fl.get(i));
                        System.out.println(art.get(i));
                        System.out.println(youurl.get(i));
                        }}});



    jl.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
           //adds a actionlistener to Jlist
            JList jl = (JList)evt.getSource();
            //if double click---
            if (evt.getClickCount() == 2) {
                int index = jl.locationToIndex(evt.getPoint());
                String url = ("https://www.youtube.com/watch"+youurl.get(index));

                if(Desktop.isDesktopSupported()){
                    Desktop desktop = Desktop.getDesktop();
                    try {
                        desktop.browse(new URI(url));
                    } catch (IOException | URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }else{
                    Runtime runtime = Runtime.getRuntime();
                    try {
                        runtime.exec("xdg-open " + url);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            } else if (evt.getClickCount() == 3) {   // Triple-click
                int index = jl.locationToIndex(evt.getPoint());
                }}});
    //listb is the "back to list" button.
    listb.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                //if u are at add window, listb will take u back to the list of songs.
                    jl.setVisible(true);
                    sp.setVisible(true);
                    add.setVisible(true);
                    edit.setVisible(true);
                    listb.setVisible(false);
                    save.setVisible(false);
                    af.setVisible(false);
                    artist.setVisible(false);
                    youtube.setVisible(false);
                    ytl.setVisible(false);
                    songg.setVisible(false);
                    arti.setVisible(false);
                }});

    save.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //takes the afart, and save it to the JList(first passed to addString)
                    String getart = artist.getText();
                    String getaf = af.getText();
                    String afart = (getaf+" - "+getart);
                    String yt = youtube.getText();
                    //pass afart to addString method
                    addString(getaf,getart,yt);
                    af.setText(null);
                    youtube.setText(null);
                    jl.requestFocus();
                    artist.setText(null);
                    //set the window back to "list of songs"
                    jl.setVisible(true);
                    sp.setVisible(true);
                    add.setVisible(true);
                    edit.setVisible(true);
                    listb.setVisible(false);
                    save.setVisible(false);
                    af.setVisible(false);
                    artist.setVisible(false);
                    youtube.setVisible(false);
                    ytl.setVisible(false);
                    songg.setVisible(false);
                    arti.setVisible(false);
                    }});
    //adds another mouselistener to jl
    jl.addMouseListener(
            new MouseAdapter(){

                public void mousePressed(MouseEvent e)  {check(e);}
                public void mouseReleased(MouseEvent e) {check(e);}
                //mouse event right click
                public void check(MouseEvent e) {
                    if (e.isPopupTrigger()) { //if the event shows the menu
                        jl.setSelectedIndex(jl.locationToIndex(e.getPoint())); //select the item
                        //creates a popupmenu.
                          JPopupMenu jpo = new JPopupMenu();
                            //creates a item that links to popupmenu.. JMenuItem works like a button
                          //this JMenuItem is a remove button
                          JMenuItem ite = new JMenuItem("remove");
                            jpo.add(ite);
                            jpo.show(jl, e.getX(), e.getY()); //and show the menu  
       //JMenuItem actionListener.         
      ite.addActionListener(
              new ActionListener(){
                  public void actionPerformed(ActionEvent e){
                     //takes selectedIndex, and remove it from the two arraylist, + the Modellist(jlist)
                      final int i = jl.getSelectedIndex();
                        if(i>=0){
                            m.removeElementAt(i);
                            fl.remove(i);
                            art.remove(i);
                            youurl.remove(i);
                        }}});}}});
//ActionListener for the back button in the edit menu
    editlist.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    jl.setVisible(true);
                    sp.setVisible(true);
                    add.setVisible(true);
                    edit.setVisible(true);
                    editlist.setVisible(false);
                    editsave.setVisible(false);
                    youtube.setVisible(false);
                    ytl.setVisible(false);
                    af.setVisible(false);
                    artist.setVisible(false);
                    songg.setVisible(false);
                    arti.setVisible(false);
                    youtube.setText(null);
                    af.setText(null);
                    artist.setText(null);
                }});
    //ActionListener for the save buttin in the edit menu
    editsave.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    //takes 2 string, and the int from getSelected index, and pass it to editSong(); method.
                    String aff = af.getText();
                    String artt = artist.getText();
                    String yte = youtube.getText();
                    final int f = jl.getSelectedIndex();
                    //System.out.println(f);
                    editSong(f,aff,artt,yte);
                    //close the edit window
                    jl.setVisible(true);
                    sp.setVisible(true);
                    add.setVisible(true);
                    edit.setVisible(true);
                    editlist.setVisible(false);
                    editsave.setVisible(false);
                    youtube.setVisible(false);
                    ytl.setVisible(false);
                    af.setVisible(false);
                    artist.setVisible(false);
                    songg.setVisible(false);
                    arti.setVisible(false);
                    youtube.setText(null);
                    af.setText(null);
                    artist.setText(null);
                }});

}   
//addString method adds new string to JList, and put them at the next avaiable index.
public void addString(String o, String l, String yt){
    //adds the songname and artistname to the arratlist.
    fl.add(o);
    art.add(l);
    youurl.add(yt);
    String p = (o+" - "+l);
    //adds the artists+songname to the jlist.
    m.addElement(p.toString()); 
}
public void editSong(int i, String song, String artt,String yte){
    String s = song;
    String a = artt;
    String sa = (s+" - "+a);
    //fl.add(i,null);
    //remove object at the indexnumber "i"(current index selected) from arraylists.
    fl.remove(i);
    art.remove(i);
    youurl.remove(i);
    //adds the new string passed in from "editsave", and put them to selectedIndex..
    fl.add(i,s);
    art.add(i,a);
    youurl.add(i,yte);
    //remove old JList element, and put in the new.
    m.removeElementAt(i);
    m.add(i,sa);
    }
公共JTextField af;
私人jlistjl;
私人按钮添加;
私有按钮编辑;
私人JButton试验;
私人JPanel jp;
私人JScrollPane sp;
私人艺术家;
私人按钮保存;
私有JButton列表b;
私人初级警务人员;
私人住宅;
私有JButton编辑保存;
私有JButton编辑列表;
私有JTextField-youtube;
私人JLabel-ytl;
私人JLabel-arti;
私人贾拉贝尔·松格;
int g;
//创建DefaultListModel..可以通过此处在JList上执行操作。例如,如果添加到JList的是m.addElement();
DefaultListModel m=新的DefaultListModel();
//创建数组列表
List fl=新的ArrayList();
列表艺术=新的ArrayList();
List youurl=new ArrayList();
公共音乐学家(){
超级(“Musiclist-Alpha”);
setLayout(空);
jl=新的JList(m);
添加(jl);
//创建滚动窗格“implements jlist”
sp=新的JScrollPane(jl);
sp.setBounds(30,30195200);
添加(sp);
//创建包含songname的文本字段
af=新的JTextField(12);
字符串afs=af.getText();
af.setBounds(20,30210,20);
添加(af);
af.setVisible(假);
//打开“添加”菜单
添加=新按钮(“添加歌曲”);
增加立根(20250100,20);
添加(添加);
//打开“编辑”菜单
编辑=新按钮(“编辑歌曲”);
编辑立根(135250100,20);
添加(编辑);
//此按钮检查art和fl(ArrayList)是否与索引匹配。
测试=新按钮(“测试”);
测试立根(300,40,80,20);
添加(测试);
//将传递艺术家字符串..的文本字段,用于添加和编辑
艺术家=新的JTextField();
艺术家挫折(20,70210,20);
添加(艺术家);
艺术家设置可见(假);
//在“添加”菜单中添加后退按钮
listb=新的JButton(“back”);
列表B.立根(135250100,20);
添加(列表B);
listb.setVisible(false);
//在“添加”菜单上添加保存按钮
保存=新的JButton(“保存”);
保存.立根(20250100,20);
添加(保存);
save.setVisible(false);
//在“编辑”菜单上添加后退按钮
editlist=新的JButton(“返回”);
editlist.setBounds(13525010020);
添加(编辑列表);
editlist.setVisible(false);
//在“编辑”菜单上添加保存按钮
editsave=新的JButton(“保存”);
editsave.setBounds(20250100,20);
添加(编辑保存);
editsave.setVisible(false);
//添加youtube文本字段
youtube=新的JTextField();
youtube.setBounds(20110120,20);
添加(youtube);
setVisible(false);
//添加jlabel
ytl=新的JLabel(“https://www.youtube.com/watch");
ytl.立根(20,90200,20);
添加(ytl);
ytl.setVisible(假);
arti=新的JLabel(“艺术家”);
第1条立根(20,50170,20);
添加(arti);
arti.setVisible(假);
songg=新的JLabel(“歌曲”);
松江(20,10170,20);
加(宋);
songg.setVisible(假);
//按钮打开添加窗口
add.addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件){
jl.setVisible(假);
sp.setVisible(假);
add.setVisible(false);
edit.setVisible(false);
listb.setVisible(true);
save.setVisible(true);
af.setVisible(真);
艺术家设置可见(真实);
setVisible(true);
ytl.setVisible(真);
songg.setVisible(真);
arti.setVisible(真实);
af.requestFocus();
}});
edit.addActionListener(
新建ActionListener(){
已执行的公共无效actionPerformed(ActionEvent事件){
System.out.println(jl.getSelectedIndex());
//检查是否有选定的索引..未选定的索引=-1。
如果(jl.getSelectedIndex()=0){
系统输出打印LN(i);
af.setText(fl.get(i));
artist.setText(art.get(i));
setext(youurl.get(i));
}}}});
//测试按钮..检查索引+歌曲+艺术家是否匹配。
test.addActionListener(
新建ActionListener(){
公共图书馆