gpt4 book ai didi

java - mysql和java jdbc从另一个类调用[运行时应用程序]

转载 作者:行者123 更新时间:2023-11-29 06:13:08 24 4
gpt4 key购买 nike

问题是由于 **app.java ** 被调用以响应插件启动,即在运行时环境中。我也尝试在运行时-EclipseApplication 中添加“mysql-connector-java-5.1.18-bin”,但它仍然无法工作。

我的 mysql/java 连接在独立时工作正常,但我无法开发连接并在从另一个类调用连接时出现异常。

我创建了一个类DataAccessHelper.java

        package dataBase;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class DataAccessHelper {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;

public void readDataBase() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");

// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);

// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
// Parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();

preparedStatement = connect
.prepareStatement("SELECT myuser, webpage, datum, summery, COMMENTS from FEEDBACK.COMMENTS");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);

// Remove again the insert comment
preparedStatement = connect
.prepareStatement("delete from FEEDBACK.COMMENTS where myuser= ? ; ");
preparedStatement.setString(1, "Test");
preparedStatement.executeUpdate();

resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeMetaData(resultSet);

} catch (Exception e) {
throw e;
} finally {
close();
}

}

private void writeMetaData(ResultSet resultSet) throws SQLException {
// Now get some metadata from the database
// Result set get the result of the SQL query

System.out.println("The columns in the table are: ");

System.out.println("Table: " + resultSet.getMetaData().getTableName(1));
for (int i = 1; i<= resultSet.getMetaData().getColumnCount(); i++){
System.out.println("Column " +i + " "+ resultSet.getMetaData().getColumnName(i));
}
}

private void writeResultSet(ResultSet resultSet) throws SQLException {
// ResultSet is initially before the first data set
while (resultSet.next()) {
// It is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
// e.g. resultSet.getSTring(2);
String user = resultSet.getString("myuser");
String website = resultSet.getString("webpage");
String summery = resultSet.getString("summery");
Date date = resultSet.getDate("datum");
String comment = resultSet.getString("comments");
System.out.println("User: " + user);
System.out.println("Website: " + website);
System.out.println("Summery: " + summery);
System.out.println("Date: " + date);
System.out.println("Comment: " + comment);
}
}

// You need to close the resultSet
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}

if (statement != null) {
statement.close();
}

if (connect != null) {
connect.close();
}
} catch (Exception e) {

}
}

}

当我直接从ma​​in.java运行此类时

    package dataBase;

public class main {
public static void main(String[] args) throws Exception {
DataAccessHelper dao = new DataAccessHelper();
dao.readDataBase();
}
}

它运行完美。但是,当我尝试调用其他类时 App.java

    package newmodulewizrd.ui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.sql.SQLException;

import javax.swing.JFrame;

import org.eclipse.jdt.core.JavaModelException;

import dataBase.DataAccessHelper;

public class App extends JFrame {

ScreenContainer screen;

public App(boolean flag)
{

}
public App() throws JavaModelException, ClassNotFoundException, SQLException{
FlowLayout fl=new FlowLayout();
this.setLayout(fl);

screen = new ScreenContainer();
DataAccessHelper con= new DataAccessHelper();
try {
con.readDataBase();
} catch (Exception e) {
e.printStackTrace();
}

newModuleWizard.bl.Module module = new newModuleWizard.bl.Module();
screen.addScreen(new Module(module,con),"module");// Screen1
screen.addScreen(new Description(module,con),"description");
screen.addScreen(new PPT(module,con), "ppt");
screen.addScreen(new Role(module,con), "role");
screen.addScreen(new MI(module,con), "MI");

this.add(screen);
this.add(new Controls(screen));

this.setSize(new Dimension(800,600));
}

public static void main(String[] args) throws JavaModelException, ClassNotFoundException, SQLException {
new App().setVisible(true);

}


}

它给出了一个异常

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

显然我的问题与

相同

Java / MySQL - How to access connection from another class?

但是,我无法从中受益,因为提供的解决方案是根据提供的代码进行的,而该代码在某种程度上无法访问[很可能链接已失效]

请帮助任何人

最佳答案

我解决了我的问题。我将其发布在这里,以便遇到相同问题的任何人也可以从中受益。

您需要在 list 中添加类路径引用。请按照以下简单步骤操作:

  1. 将文件夹“lib”添加到您的应用程序
  2. 将“mysql-connector-java-5.1.18-bin”放入lib
  3. 现在打开“MANIFEST.MF”并转到“RUNTIME”选项卡
  4. 在右下角,你会看到“classpath”;点击“添加”
  5. 现在添加文件夹 lib [在步骤 1 中创建] 以及 jar 文件

这样,每当运行时 EclipseApplication/OSGi 应用程序启动时,该 jar 文件也会随之导出。因此连接也将在那里可用。

关于java - mysql和java jdbc从另一个类调用[运行时应用程序],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8126998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com