嗨我正在使用netbeans + glassfish 我正在尝试运行此代码: 我只创建没有任何表的数据库(通过运行此代码我想创建表并保留我的对象)
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class Casino extends JFrame implements ActionListener {
private JButton start, settings, scenario, music;
/**
* Constructor method
*/
public Casino(){
JPanel mainUI, startUI, settingsUI, scenarioUI, blackjackUI, oddorevenUI, tcmUI, overorunderUI, slotsUI;
JPanel menus = new JPanel(new CardLayout());
CardLayout GUI = (CardLayout) menus.getLayout();
mainUI = new JPanel();
getContentPane().add(mainUI);
mainUI.setBackground(new Color(53, 9, 9));
//Background items
JLabel title = new JLabel(new ImageIcon("title.png"));
title.setBounds(0,-280,780,700);
mainUI.add(title);
JLabel border = new JLabel(new ImageIcon("mainscreenborder.png"));
border.setBounds(0, 180, 780, 700);
mainUI.add(border);
//Main menu buttons
settings = new JButton();
ImageIcon s = new ImageIcon("settings-button.png");
settings.setBounds(320, 200, 122, 63);
settings.setIcon(s);
mainUI.add(settings);
music = new JButton();
ImageIcon m = new ImageIcon("music-button.png");
music.setBounds(320, 268, 122, 63);
music.setBackground(new Color(53, 9, 9));
music.setIcon(m);
mainUI.add(music);
scenario = new JButton();
ImageIcon sc = new ImageIcon("scenario-button.png");
scenario.setBounds(320, 336, 122, 63);
scenario.setBackground(new Color(53, 9, 9));
scenario.setIcon(sc);
mainUI.add(scenario);
start = new JButton();
ImageIcon st = new ImageIcon("start-button.png");
start.setBounds(320, 404, 122, 63);
start.setBackground(new Color(53, 9, 9));
start.setIcon(st);
mainUI.add(start);
menus.add(mainUI, "Main Menu");
GUI.show(menus, "Main Menu");
setSize(780, 700);
setResizable(false);
setLayout(GUI);
setTitle("White Lily Casino");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Casino wlc = new Casino();
}
}
但我收到了这个错误:
public static void main(String[] args) {
SmartphoneService ss = new SmartphoneService();
Smartphone smart = new Smartphone(0, 0, null, null, null);
ss.create(smart);
}
我的persistence.xml:
Unable to lookup JNDI name
我的智能手机服务:
<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/mysql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
我检查了连接池ping(Ping成功)
感谢您的帮助
答案 0 :(得分:1)
您正在混淆JavaSE和JavaEE环境。
您的数据源看起来像是在Glassfish(Java EE环境)上配置的。因此,JNDI名称java:comp/env/jdbc/mysql
仅在Java EE上下文中可用。
您的SmartphoneService
正在Java SE上下文中运行(通过public static void main()
方法。当您尝试查找java:comp/env/jdbc/mysql
时,它不会是因为DataSource只存在于Glassfish(Java EE)环境中。
您需要从注册资源的相同上下文中执行JNDI查找。我的建议是让您的SmartphoneService
代码在Glassfish上运行。有很多方法可以驱动它 - EJB,Servlet等......