我正在尝试创建一个简单的java程序并尝试在其上实现JDBC。 但我得到一个异常ClassNotFoundException com.mysql.jdbc.Driver
这是logcat:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at vehicles.Vehicles.main(Vehicles.java:43)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
这是我的java代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vehicles;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.jws.Oneway;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Akshay Sood
*/
public class Vehicles extends JFrame{
public static JLabel manfName,cost,modelNo,year,color;
public static JTextField colorTXT,costTXT,yearTXT,modelNoTXT,manfNameTXT;
public static JButton submit,reset;
static String Manufacturer,ModelNo,Year,Color,Cost;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/VEHICLE";
static final String USER = "root";
static final String PASS = "8851";
static ResultSet resultSet;
static Statement statement = null;
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
statement = conn.createStatement();
init();
JFrame jFrame = new JFrame();
jFrame.setTitle("Vehicle Registration");
jFrame.setSize(800, 1600);
jFrame.setLayout(null);
jFrame.show();
jFrame.add(manfName);
jFrame.add(modelNo);
jFrame.add(year);
jFrame.add(color);
jFrame.add(cost);
jFrame.add(manfNameTXT);
jFrame.add(modelNoTXT);
jFrame.add(yearTXT);
jFrame.add(colorTXT);
jFrame.add(costTXT);
jFrame.add(submit);
jFrame.add(reset);
}
private static void init() {
manfName = new JLabel();
manfName.setText("Manufacturer: ");
manfName.setBounds(10, 10, 150, 10);
modelNo = new JLabel();
year = new JLabel();
color = new JLabel();
cost = new JLabel();
modelNo.setText("Model No :");
year.setText("Year of Purchase:");
color.setText("Color:");
cost.setText("Cost:");
modelNo.setBounds(10, 40, 150, 10);
year.setBounds(10, 70, 150, 10);
color.setBounds(10, 100, 150, 10);
cost.setBounds(10, 130, 150, 10);
manfNameTXT = new JTextField();
modelNoTXT = new JTextField();
yearTXT = new JTextField();
colorTXT = new JTextField();
costTXT = new JTextField();
manfNameTXT.setBounds(300, 10, 150, 20);
modelNoTXT.setBounds(300, 40, 150, 20);
yearTXT.setBounds(300, 70, 150, 20);
colorTXT.setBounds(300, 100, 150, 20);
costTXT.setBounds(300, 130, 150, 20);
submit = new JButton();
reset = new JButton();
submit.setText("Submit");
reset.setText("Reset");
submit.setBounds(100, 200, 100, 20);
reset.setBounds(200, 200, 100, 20);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Manufacturer = manfNameTXT.getText().toString();
ModelNo = modelNoTXT.getText().toString();
Year= yearTXT.getText().toString();
Color= colorTXT.getText().toString();
Cost= costTXT.getText().toString();
System.out.println(Manufacturer + " " + ModelNo + " " + Year + " " + Color + " " + Cost);
try {
resultSet = statement.executeQuery("insert into vehicle values(" + Manufacturer + "," + ModelNo + "," + Year + "," + Color + ","+Cost+")");
} catch (SQLException ex) {
Logger.getLogger(Vehicles.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
我不知道出了什么问题。请帮忙
答案 0 :(得分:0)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class myTest {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/so_gibberish"; // **** MODIFY db name (my db_name is so_gibberish)
String user = "username"; // **** MODIFY
String password = "my_password"; // **** MODIFY
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
System.out.println("------------------------------");
rs = st.executeQuery("select version()");
if (rs.next()) {
System.out.println(rs.getString(1));
System.out.println("------------------------------");
}
rs = st.executeQuery("select id,myCode from thingws");
while (rs.next()) {
System.out.println(rs.getInt(1)+": "+rs.getString(2));
}
System.out.println("------------------------------");
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(myTest.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(myTest.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
}
我在一个文件夹(windows或linux)中编译它
C:\dev\java8\quick_java_mysql_test>javac myTest.java
C:\dev\java8\quick_java_mysql_test>
(no errors)
我在该文件夹下有一个依赖项的dep文件夹
Directory of C:\dev\java8\quick_java_mysql_test\dep
08/26/2015 10:29 AM <DIR> .
08/26/2015 10:29 AM <DIR> ..
03/17/2015 05:21 AM 968,670 mysql-connector-java-5.1.35-bin.jar
1 File(s) 968,670 bytes
我使用命令行classpath(cp)选项标志来运行它来获取类和dep文件夹:
C:\dev\java8\quick_java_mysql_test>java -cp ".;dep/mysql-connector-java-5.1.35-bin.jar" myTest
------------------------------
5.6.24-log
------------------------------
Oct 17, 2015 11:35:25 AM myTest main
SEVERE: Table 'so_gibberish.thingws' doesn't exist
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'so_gibberish.thingws' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.Util.getInstance(Util.java:372)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2531)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2489)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1446)
at myTest.main(myTest.java:31)
未使用。我的这个策略是让它在项目文件夹下具体项目。其他人选择其他策略,我经常使用classpath变量。
您还可以拥有一个Web容器,例如tomcat和web-inf等。
这只是一个快速的命令行健全性检查。
上面的程序在连接上成功,转出了mysql的版本,然后就完全没有使用不再存在的表了
如果我没有运行程序时所描述的良好的类路径(cp)设置(@JonSkeet的内容),那么在我的情况下我会遇到你的错误。
这里有一个类路径环境变量:
classpath=.;c:\javadeps\*
与上面相同,驱动程序在我的常用c:\ javadeps
中被选中