This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes using java to write to MySQL , multiple objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "using java to write to MySQL , multiple objects" Watch "using java to write to MySQL , multiple objects" New topic
Author

using java to write to MySQL , multiple objects

Scott Pike
Greenhorn

Joined: Jan 17, 2007
Posts: 12
I've been trying for the last week to write java code that would write an arraylist of objects to a MySQL database. but nothing seems to work. it only went through the while loop once and then gets out of the loop somehow without going through the arraylist. So I changed it so that before the arraylist is passed to the code class it, only one instance is passed to the class and that is written out and it goes through the loop and through every array list element before the loop in the Junit is done. but every time the method in the class tries to write out it puts out a database cant connect error.

Question:
1. How , if I have multiple objects, do I write mutiple objects out to the database with out hard coding it in the class method?

2. What is going wrong with my program that causes it to have an error when it writes out?

Here is Junit:



package org.junit.internal.runners;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.Failure;

public class TestClassMethodsRunner extends Runner implements Filterable, Sortable {
private final List<Method> fTestMethods;
private final Class<?> fTestClass;

// This assumes that some containing runner will perform validation of the test methods
public TestClassMethodsRunner(Class<?> klass) {
fTestClass= klass;
fTestMethods= new TestIntrospector(getTestClass()).getTestMethods(Test.class);
}

@Override
public void run(RunNotifier notifier) {
if (fTestMethods.isEmpty())
testAborted(notifier, getDescription());
for (Method method : fTestMethods)
invokeTestMethod(method, notifier);
}

private void testAborted(RunNotifier notifier, Description description) {
// TODO: duped!
// TODO: envious
notifier.fireTestStarted(description);
notifier.fireTestFailure(new Failure(description, new Exception("No runnable methods")));
notifier.fireTestFinished(description);
}

@Override
public Description getDescription() {
Description spec= Description.createSuiteDescription(getName());
List<Method> testMethods= fTestMethods;
for (Method method : testMethods)
spec.addChild(methodDescription(method));
return spec;
}

protected String getName() {
return getTestClass().getName();
}

protected Object createTest() throws Exception {
return getTestClass().getConstructor().newInstance();
}

protected void invokeTestMethod(Method method, RunNotifier notifier) {
Object test;
try {
test= createTest();
} catch (Exception e) {
testAborted(notifier, methodDescription(method));
return;
}
createMethodRunner(test, method, notifier).run();
}

protected TestMethodRunner createMethodRunner(Object test, Method method, RunNotifier notifier) {
return new TestMethodRunner(test, method, notifier, methodDescription(method));
}

protected String testName(Method method) {
return method.getName();
}

protected Description methodDescription(Method method) {
return Description.createTestDescription(getTestClass(), testName(method));
}

public void filter(Filter filter) throws NoTestsRemainException {
for (Iterator iter= fTestMethods.iterator(); iter.hasNext() {
Method method= (Method) iter.next();
if (!filter.shouldRun(methodDescription(method)))
iter.remove();
}
if (fTestMethods.isEmpty())
throw new NoTestsRemainException();
}

public void sort(final Sorter sorter) {
Collections.sort(fTestMethods, new Comparator<Method>() {
public int compare(Method o1, Method o2) {
return sorter.compare(methodDescription(o1), methodDescription(o2));
}
});
}

protected Class<?> getTestClass() {
return fTestClass;
}
}





HERE is the Code for Class SGL imput output


package CIT310;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

public class mySQLio {
public ArrayList<PlayerBean> write(String path, ArrayList<PlayerBean> PlayerRecord)
{
Connection conn = null;
try
{

String url = path;
String userName = "user";
String passWord = "password";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(
url,userName, passWord);
System.out.println("Database connection established 2");
//int i = 0;
//Iterator it = PlayerRecord.iterator();
Statement swrite = conn.createStatement();

int i = 0;
PlayerBean bPlayer = PlayerRecord.get(i);
//String Uname = bPlayer.getUName();
//String pWord = bPlayer.getPWord();
//int level = bPlayer.getLevel();
//i++;
swrite.executeUpdate(
"INSERT INTO players(2, 3, 4) VALUES (' "
+ bPlayer.getUName() + "','"
+ bPlayer.getPWord() + "','"
+ bPlayer.getLevel() + ")");


//i++;
//"INSERT INTO player (Uname, Pword, level) VALUES ('joe','password',1)";

}catch(Exception e){
System.err.println ("Cannot connect to database server 2");
}
finally{
if(conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated 2");
}
catch(Exception e){/*ignore close errors*/}
}
}
return PlayerRecord;
}
public ArrayList<PlayerBean> read(String path, ArrayList<PlayerBean> p)
{
return null;
}
}

What is wrong with this code?
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi,

this is really wrong, you will never know what went wrong


At a minimum, print the class of exception and the message.

Also: use PreparedStatement instead of Statement, you will not need the quotes and double quotes anymore.


Herman
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using java to write to MySQL , multiple objects