我有一个运行良好的 java 类(我的意思是 mysql 连接到 sql 是成功的)但是在同一个包中,我有另一个类。我试图从那里创建连接对象,然后打算将连接用于各种目的。这是工作正常的类(我将函数名从 public static void main(String[] args) 更改为 connect()。我真的不确定为什么无法从其他文件调用连接。
db.java
import java.sql.*;
import java.util.Properties;
public class db
{
// The JDBC Connector Class.
String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
String CONNECTION =
"jdbc:mysql://127.0.0.1/news";
public Connection Connect() throws
ClassNotFoundException,SQLException
{
System.out.println(dbClassName);
// Class.forName(xxx) loads the jdbc classes and
// creates a drivermanager class factory
Class.forName(dbClassName);
// Properties for user and password. Here the user and password are both 'paulr'
Properties p = new Properties();
p.put("user","root");
p.put("password","akshay");
// Now try to connect
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println("It works !");
return c;
}
}
从这些类中,我尝试调用该函数:
import java.sql.*;
public class findl {
public static void main(String[] args)
{
System.out.println("hi");
}
public Connection conn(){
db dbs = new db();
Connection clr = dbs.Connect();
return clr;
}
}
脚注:错误是在线
Connections clr = dbs.connect()
和日食说
1)unhandled exception type sqlexception
2)unhandled classnotfoundexception.
如果加载 mysql.jar 有问题,那么它不应该首先工作(在原始类中)。请告诉我我错过了什么。谢谢。