我目前正在使用以下代码连接到 mysql 数据库,但项目经理表示我必须使用 Spring 框架来处理连接。
我怎么能做到这一点?
使用 Spring 处理数据库连接会提高整个系统的质量吗?
这是该类的示例
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class SomeClass {
Connection connection = null;
ResultSet resultSet;
Statement state;
ArrayList aList = new ArrayList();
public void connectToDatabase(String databaseName)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/" + databaseName, "username", "password");
state = (Statement) connection.createStatement();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
private void populateAList() {
try
{
connectToTable("testDB");
resultSet = state.executeQuery("SELECT listItems FROM tblListItems WHERE ID = '" + theIDYouWant + "'");
while(resultSet.next())
{
aList.add(resultSet.getString("listItems"));
}
resultSet.close();
state.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
}