这个问题在这里已经有答案了:
如何解决classnotfoundexception(22个答案)
连接到数据库时出错:(使用org.gjt.mm.mysql.driver类)(3个答案)
上个月关门了。
我做了一个简单的表单,让用户输入客户数据,它将文本字段中的数据插入到我运行的microsoftsqlservermanagementstudio18的数据库中。数据库正在我的计算机上运行。我的代码的驱动程序部分有问题,它不断抛出异常。非常感谢你对我所做错事的任何帮助。这是我每次运行程序并点击提交按钮时收到的以下消息:抛出异常:com.microsoft.sqlserver.jdbc.sqlserverdriver java.lang.classnotfoundexception:com.microsoft.sqlserver.jdbc.sqlserverdriver问题的完整堆栈跟踪:atjava.base/jdk.internal.loader.builtinclassloader.loadclass(builtinclassloader。java:602)在java.base/jdk.internal.loader.classloaders$appclassloader.loadclass(classloaders。java:178)在java.base/java.lang.classloader.loadclass(classloader。java:521)位于java.base/java.lang.class.forname0(本机方法)java.base/java.lang.class.forname(类。java:333)在dbfe2.actionperformed(dbfe2。java:119)在java.desktop/javax.swing.abstractbutton.fireactionperformed(abstractbutton。java:1967)在java.desktop/javax.swing.abstractbutton$handler.actionperformed(abstractbutton。java:2308)在java.desktop/javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel。java:405)在java.desktop/javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel。java:262)在java.desktop/javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener。java:279)在java.desktop/java.awt.component.processmouseevent(组件。java:6636)在java.desktop/javax.swing.jcomponent.processmouseevent(jcomponent。java:3342)在java.desktop/java.awt.component.processevent(component。java:6401)在java.desktop/java.awt.container.processevent(container。java:2263)在java.desktop/java.awt.component.dispatcheventimpl(组件。java:5012)在java.desktop/java.awt.container.dispatcheventimpl(container。java:2321)在java.desktop/java.awt.component.dispatchevent(component。java:4844)在java.desktop/java.awt.lightweightdispatcher.RetargeMouseeEvent(容器。java:4918)在java.desktop/java.awt.lightweightdispatcher.processmouseevent(容器。java:4547)在java.desktop/java.awt.lightweightdispatcher.dispatchevent(container。java:4488)在java.desktop/java.awt.container.dispatcheventimpl(container。java:2307)在java.desktop/java.awt.window.dispatcheventimpl(窗口。java:2762)在java.desktop/java.awt.component.dispatchevent(组件。java:4844)在java.desktop/java.awt.eventqueue.dispatcheventimpl(eventqueue。java:772)在java.desktop/java.awt.eventqueue$4.run(eventqueue。java:721)在java.desktop/java.awt.eventqueue$4.run(eventqueue。java:715)在java.base/java.security.accesscontroller.doprivileged(accesscontroller。java:391)在java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain。java:85)在java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain。java:95)在java.desktop/java.awt.eventqueue$5.run(eventqueue。java:745)在java.desktop/java.awt.eventqueue$5.run(eventqueue。java:743)在java.base/java.security.accesscontroller.doprivileged(accesscontroller。java:391)在java.base/java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain。java:85)在java.desktop/java.awt.eventqueue.dispatchevent(事件队列)。java:742)在java.desktop/java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread。java:203)在java.desktop/java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread。java:124)在java.desktop/java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread。java:113)在java.desktop/java.awt.eventdispatchthread.pumpevents(eventdispatchthread。java:109)在java.desktop/java.awt.eventdispatchthread.pumpevents(eventdispatchthread。java:101)在java.desktop/java.awt.eventdispatchthread.run(eventdispatchthread。java:90)
package Assignments;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;//import all
import java.sql.*;//need this to make database connection
public class DataBaseFormExample extends JPanel implements ActionListener
{
private JLabel FirstNameLabel;
private JTextField FirstNameTextField;
private JLabel LastNameLabel;
private JTextField LastNameTextField;
private JLabel PhoneNumLabel;
private JTextField PhoneNumTextField;
private JLabel StreetAddressLabel;
private JTextField StreetAddressTextField;
private JLabel StateLabel;
private JTextField StateTextField;
private JLabel CityLabel;
private JTextField CityTextField;
private JLabel ZipLabel;
private JTextField ZipTextField;
private JLabel GenderLabel;
private JTextField GenderTextField;
private JLabel DOBLabel;
private JTextField DOBTextField;
private JLabel MaritalStatusLabel;
private JTextField MartialStatusTextField;
//constructor
public DataBaseFormExample()
{
//intializing components for form below
FirstNameLabel = new JLabel("Enter the first name of Customer below:");
FirstNameTextField = new JTextField("FirstName");
FirstNameTextField.setColumns(10);
LastNameLabel = new JLabel("Enter the first name of Customer below:");
LastNameTextField = new JTextField("LastName");
LastNameTextField.setColumns(10);
PhoneNumLabel = new JLabel("Enter the Phone Number of Customer below(EX:123-456-7890):");
PhoneNumTextField = new JTextField("123-456-7890");
PhoneNumTextField.setColumns(10);
StreetAddressLabel = new JLabel("Enter the street address of Customer below:");
StreetAddressTextField = new JTextField("123 easy street");
StreetAddressTextField.setColumns(10);
StateLabel = new JLabel("Enter the state of Customer below(EX:PA):");
StateTextField = new JTextField("PA");
StateTextField.setColumns(10);
CityLabel = new JLabel("Enter the city of Customer below(EX:Pittsburgh):");
CityTextField = new JTextField("Pittsburgh");
CityTextField.setColumns(10);
ZipLabel = new JLabel("Enter the zip of Customer below(EX:12345):");
ZipTextField = new JTextField("12345");
ZipTextField.setColumns(10);
GenderLabel = new JLabel("Enter the gender of Customer below(EX:M or F):");
GenderTextField = new JTextField("M");
GenderTextField.setColumns(10);
DOBLabel = new JLabel("Enter the DOB of Customer below(EX:12/03/5678):");
DOBTextField = new JTextField("12/34/2010");
DOBTextField.setColumns(10);
MaritalStatusLabel = new JLabel("Enter the martial status of Customer below(EX: Single / Married):");
MartialStatusTextField = new JTextField("Single");
MartialStatusTextField.setColumns(10);
JButton submitBTN = new JButton("Submit info");
submitBTN.addActionListener(this);
//add components to form
add(FirstNameLabel);
add(FirstNameTextField);
add(LastNameLabel);
add(LastNameTextField);
add(PhoneNumLabel);
add(PhoneNumTextField);
add(StreetAddressLabel);
add(StreetAddressTextField);
add(StateLabel);
add(StateTextField);
add(CityLabel);
add(CityTextField);
add(ZipLabel);
add(ZipTextField);
add(GenderLabel);
add(GenderTextField);
add(DOBLabel);
add(DOBTextField);
add(MaritalStatusLabel);
add(MartialStatusTextField);
add(submitBTN);
}
//driver method
public static void CreateAndShowContent()
{
//frame of calculator
JFrame window = new JFrame("Customer Information Input Form");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes when we click the red x
DataBaseFormExample content = new DataBaseFormExample ();
content.setOpaque(true);
window.setContentPane(content);
//size it to fit components
window.setSize(500,330);
//show it
window.setVisible(true);
}
//insert when the button submit is pressed
public void actionPerformed(ActionEvent e)
{
try
{
//try inserting data to database
String JDBC_DRIVER = "org.gjt.mm.mysql.Driver";
String DB_URL = "jdbc:mysql://localhost/Giant_Snail_Grocery";
String UserName = "username";
String UserPass = "password";
Connection conn = null;
Statement statement = null;
Class.forName(JDBC_DRIVER);
System.out.println("Class.forName(driver) accepted....");
conn = DriverManager.getConnection(DB_URL,UserName,UserPass);
System.out.println("Connection Made....");
statement = conn.createStatement();
System.out.println("Statement object created from conn....");
//String sql = "INSERT INTO CUSTOMER " +"VALUES('Todd','Packer','724-345-9876','134 west end road','PA','Pittsburgh','12356','M','12/21/1995','married');";
String sql2 = "INSERT INTO CUSTOMER " + "VALUES("+ FirstNameTextField.getText() + " , " + LastNameTextField.getText() + ", " + PhoneNumTextField.getText() + ", " + StreetAddressTextField.getText() + ", " + StateTextField.getText() + ", " + CityTextField.getText() + ", " + ZipTextField.getText() + "," + GenderTextField.getText() + ", " + DOBTextField.getText() + ", " + MartialStatusTextField.getText() + ");";
statement.executeUpdate(sql2);
System.out.println("Insert successful to database....");
if(statement != null)
{
conn.close();
System.out.println("Connection closed to database....");
}
}
catch(Exception p)
{
//catch any exceptions
System.out.println("Exception thrown: " + p.getMessage());
}
}
//Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
CreateAndShowContent();
}
});
}
}
1条答案
按热度按时间sr4lhrrt1#
你是说这是mssql。因此,您必须在buildpath中添加jar文件,或者将其作为maven依赖项添加(如果它是mvn项目)。