代码有什么问题,调试时有很多错误.我正在编写一个单例类的代码来连接数据库mysql.
这是我的代码
package com.glomindz.mercuri.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySingleTon {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
private static MySingleTon myObj;
private Connection Con ;
private MySingleTon() {
System.out.println("Hello");
Con= createConnection();
}
@SuppressWarnings("rawtypes")
public Connection createConnection() {
Connection connection = null;
try {
// Load the JDBC driver
Class driver_class = Class.forName(driver);
Driver driver = (Driver) driver_class.newInstance();
DriverManager.registerDriver(driver);
connection = DriverManager.getConnection(url …Run Code Online (Sandbox Code Playgroud) 我不知道如何使用hashmap和while循环从我的数据库中检索数据.请帮助我.
我的代码是
package com.glomindz.mercuri.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import com.glomindz.mercuri.util.MySingleTon;
public class UserServicesDAO {
private Connection connection;
public UserServicesDAO() {
//connection = new MySingleTon().getConnection();
connection = MySingleTon.getInstance().getConnection();
}
public void get_all_data() {
HashMap<Integer, String> result = new HashMap<Integer, String>();
String query = "SELECT * FROM spl_user_master";
try {
PreparedStatement stmt = connection.prepareStatement(query);
boolean execute = stmt.execute();
System.out.println(execute);
ResultSet resultSet = stmt.getResultSet();
System.out.println(resultSet.getMetaData());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud)