Java 创建多个数据类型的ArrayList时出错
这段代码连接Mongodb和Mysql数据库。mysql表中有两个字段Java 创建多个数据类型的ArrayList时出错,java,mysql,mongodb,Java,Mysql,Mongodb,这段代码连接Mongodb和Mysql数据库。mysql表中有两个字段thing(String)和qty(int)。我希望通过创建一个名为Names的对象类,将string和int作为字段成员,然后使用ArrayList,在mongodb中以与string和int相同的格式显示。我在第93行和第94行(在方法sqldbRead stock_list.add(..)中)发现了一个bug,因为我猜我没有使用完美的格式,或者代码中可能存在一些不同的错误。好几个小时都想不出来。任何帮助都将不胜感激!多谢
thing(String)
和qty(int)
。我希望通过创建一个名为Names的对象类,将string和int作为字段成员,然后使用ArrayList,在mongodb中以与string和int相同的格式显示。我在第93行和第94行(在方法sqldbRead stock_list.add(..)
中)发现了一个bug,因为我猜我没有使用完美的格式,或者代码中可能存在一些不同的错误。好几个小时都想不出来。任何帮助都将不胜感激!多谢各位
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mysql.jdbc.Statement;
public class connect {
// this opens and inserts data into SQL database, takes columns variables( string, int)
public void sqldbInsert(String thing, int qty) throws ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Creating a connection");
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","Testuser","");
System.out.println("Getting access to the database...");
}
catch(SQLException e){
System.err.println(e.getErrorCode()+" :"+e.getSQLState()+" :"+e.getMessage());
if(e.getErrorCode()==1049){
//create database here with its own try catch block
try{
//Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/","Testuser","");
Statement stmt = (Statement) con.createStatement();
String sql = "CREATE DATABASE test";
stmt.executeUpdate(sql);
System.out.println("Database created successfully");
} catch(SQLException e1){
System.err.println(e1.getErrorCode()+" :"+e1.getSQLState()+" :"+e1.getMessage());
}
try{
//Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","Testuser","");
System.out.println("Inserting values into the table");
PreparedStatement stmt = con.prepareStatement("INSERT INTO DETAILS (thing,qty)VALUES(?,?)");
stmt.setString(1, thing);
stmt.setInt(2, qty);
stmt.executeUpdate();
stmt.close();
con.close();
System.out.println("It's working!!");
}
catch(SQLException e1){
e1.printStackTrace();
}
}
}
}
// also open , and reads the sql table, using specific select statement using qty field
public void sqldbRead() throws SQLException, ClassNotFoundException{
// open, sql select, and close
//returns array of objects
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","Testuser","");
PreparedStatement statement = con.prepareStatement("select thing,qty from things where qty > 4");
ResultSet result = statement.executeQuery();
ArrayList<Names> stock_list = new ArrayList<Names>();
while(result.next()){
stock_list.add(result.getString(1));
stock_list.add(result.getInt(2));
}
Names[] stockArr = new Names[stock_list.size()];
for(Names s : stockArr)
System.out.println(s);
mongodbInsert(stockArr);
con.close();
}
// open mongo db, gets a array of objects, and inserts as Field:value pairs
public void mongodbInsert(Names[]s){
// open, insert collection into a known collection, close
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "First" );
System.out.println("Connect to database successfully");
try{
DBCollection coll = db.getCollection("begin");
System.out.println("Collection created successfully");
for(int i=0;i<s.length-1;i++){
BasicDBObject doc = new BasicDBObject().append(s[i].toString(),s[i+1]);
coll.insert(doc);
i++;
}
System.out.println("Document inserted successfully");
}catch(Exception e){
System.err.print(e.getMessage()+"Collection not found");
}
}catch(Exception e){
System.err.print(e.getMessage()+"Not connected to the database");
}
}
// read out specific mongodb document, takes string , int ( fieldname:value)
public void mongodbRead(){
// open, find, close
try{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "First" );
System.out.println("Connect to database successfully");
try{
DBCollection coll = db.getCollection("begin");
System.out.println("Collection created successfully");
Set<String> colls = db.getCollectionNames();
for (String s1 : colls) {
System.out.println(s1);
}
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("chair", "11");
DBCursor cursor = coll.find(whereQuery);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
}
catch(Exception e){
System.err.print(e.getMessage()+"Collection not found");
}
} catch(Exception e){
System.err.print(e.getMessage()+"Not connected to the database");
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException{
connect conn = new connect();
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string and a value to insert into the database");
String s = scan.next();
int i = scan.nextInt();
conn.sqldbInsert(s,i);
conn.sqldbRead();
conn.mongodbRead();
}
}
stock_list.add()需要一个Names对象,您要传递一个字符串和一个整数。
试一试
另外,在接下来的几行中,当您创建一个新数组时,您会遇到一个bug,但随后您会将其保留为空,并进行一次修改
for(Names s : stockArr)
这里stockArr的大小合适,但它是空的
for(Names s : stock_list)
这可能会起作用,或者在你进入循环之前,你可以调用列表中的数组函数。我现在不能检查它,但我认为它是这样的
stock_list.toArray();
错误是什么将有助于。。。没有可匹配的行号。感谢Neso的回复。在使用stock_list.add(新名称(result.getInt(),result.getString());正如您所说,这行代码没有错误,但我一直在显示输出并将值传递给mongodb。我添加了stockArr=stock_list.toArray(stockArr);所以现在看起来像Names[]stockArr=new Names[stock_list.size()];stockArr=stock_list.toArray(stockArr);for(名称s:stockArr)System.out.println(s);mongodbInsert(stockArr);con.close();nono-don-do stockArr=stock_list.toArray()因为我不知道它返回什么,但它修改了作为参数输入的数组。如果它返回数组,它将不再是以前的同一个对象(可能是错误的,因为我不知道它,但我现在将查看它:D)编辑:我已经查找了t,它似乎返回了一个填充了集合内容的数组,因此我将再次检查您的代码=)是的,toArray无法工作,因为它再次将其转换为数组。有什么建议吗/好的,您需要重写Names中的toString()方法,并返回一个表示要输出内容的字符串。请在循环中设置断点,并检查数组是否具有正确的内容。同时删除“stockArr=”并仅使用stockArr列表。在文档中,您可以读取参数->的说明,该参数是存储此集合元素的数组(如果该数组足够大);否则,将为此目的分配一个相同运行时类型的新数组
for(Names s : stock_list)
stock_list.toArray();