数据库连接在 jar 中不起作用,但在 eclipse 中起作用
我在 MYSQL 中创建了一个简单的数据库,我试图从 Eclipse 编写的 java 程序中提取数据。当我在 Eclipse 中运行该程序时,它可以很好地提取数据,但是当我将程序导出到可运行的 JAR 中时,它就无法工作!
我通过在构建路径中使用“添加外部存档”功能添加了 mysql 连接器,并使用
System.out.println("classpath = " + System.getProperty("java.class.path"));
以下命令检查了它是否存在: classpath = C:\Users\Andy\workspace\test2\bin;C:\Users\Admin\Desktop\mysql-connector-java-5.1.16\mysql-connector-java-5.1.16-bin.jar
我也有打开导出的 JAR 文件,可以看到连接器已添加。
这是我正在使用的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class test
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection dbConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cambridge","root","P@ssw0rd");
System.out.println("classpath = " + System.getProperty("java.class.path"));
Statement stmt = dbConn.createStatement();
String strSQL = "Select * from customer_details";
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
int nCol = rsmd.getColumnCount();
while(rs.next())
{
for(int col=1; col<=nCol; col++)
{
System.out.print(rs.getString(col));
JOptionPane.showMessageDialog(null, rs.getString(col));
}
System.out.println();
}
System.out.println();
dbConn.close();
}
catch (Exception excp)
{
excp.printStackTrace();
}
}
}
提前感谢您提供的任何帮助!
I have created a simple database in MYSQL, which I am trying to pull data from in a java program written in Eclipse. When I run the program in eclipse, it pulls out the data fine, but when I export the program into a runnable JAR, it just won't work!!
I have addedthe mysql connector by using the "add External Archive" feature in the build path, and have checked that its there by using
System.out.println("classpath = " + System.getProperty("java.class.path"));
which gives the results:
classpath = C:\Users\Andy\workspace\test2\bin;C:\Users\Admin\Desktop\mysql-connector-java-5.1.16\mysql-connector-java-5.1.16-bin.jar
I have also opened up the exported JAR file and have seen that the connector has been added.
Here is the code that I am using:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class test
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection dbConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cambridge","root","P@ssw0rd");
System.out.println("classpath = " + System.getProperty("java.class.path"));
Statement stmt = dbConn.createStatement();
String strSQL = "Select * from customer_details";
ResultSet rs = stmt.executeQuery(strSQL);
ResultSetMetaData rsmd = rs.getMetaData();
int nCol = rsmd.getColumnCount();
while(rs.next())
{
for(int col=1; col<=nCol; col++)
{
System.out.print(rs.getString(col));
JOptionPane.showMessageDialog(null, rs.getString(col));
}
System.out.println();
}
System.out.println();
dbConn.close();
}
catch (Exception excp)
{
excp.printStackTrace();
}
}
}
Thanks in advance for any help you can offer!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于明白了!
我执行了以下操作:
然后单击“新建”,输入名称,然后单击“文件”并导航到 mysql 连接器,然后单击“确定”!
不完全确定为什么这有效,但是当我做了几乎相同的事情时,但对于单个项目没有,但它有效,这就是主要的事情!
感谢所有回复并为我提供帮助的人。我花了一段时间,但我最终到达了那里! :o)
FINALLY GOT IT!!!
I did the following:
then clicked on new, entered a name, then clicked on file and navigated to the mysql connector, and then hit ok!!
Not entirely sure why this worked, but when I did pretty much the same, but for an individual project didn't, but it worked, and thats the main thing!!
Thanks to everyone who responded and offered me some help. It took me a while, but I got there in the end!! :o)