gpt4 book ai didi

java - 连接java和mysql

转载 作者:行者123 更新时间:2023-11-30 11:50:47 25 4
gpt4 key购买 nike

我已经使用下面的代码在java和mysql之间进行连接,但是它没有找到 JDBC_DRIVER。我该如何解决这个问题?

代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*This code connects to the mysql database*/
public class mysqlconn
{
public Connection con = null;
public Statement statement= null;
public String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public String username = "root",password = "taskinn432175";
static String dbname = "jdbc:mysql://localhost/shopkeeper";
String sql="select * from shopkeeper";
String dbtime;

public mysqlconn(String userd , String passd){
this.password = passd;
this.username= userd;
}

public void Connect() {
try {
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(dbname,username,password);
statement= con.createStatement();
System.out.println ("Database connection established");
System.out.println("capturing from database");
ResultSet rs=statement.executeQuery(sql);
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"not connected"+e.getMessage());
}
}

public static void main( String args[] )
{
mysqlconn conn=new mysqlconn("taskin","taskinn432175");
conn.Connect();
}
}

最佳答案

确保您拥有 JDBC drivers for mySql在你的类路径中。

一种方法(对于像这样的简单测试程序):

java -cp .;path_to_jdbc_connector; mysqlconn

关于java - 连接java和mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7785697/

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