Tuesday, September 14, 2010

Re: MySQL database connection

Very nice! So what's the problem? You should consider using springs JdbcTemplate.

-----Original Message-----
From: Romeo Calota <kicsyromy@gmail.com>
Sender: google-web-toolkit@googlegroups.com
Date: Tue, 14 Sep 2010 09:14:04
To: Google Web Toolkit<google-web-toolkit@googlegroups.com>
Reply-To: google-web-toolkit@googlegroups.com
Subject: MySQL database connection

I want to establish a connection to a local MySQL database. I've
written the code for connecting in the server package. And created the
RPC connections for server-client communication. The commnication
between the two works and the code for connection to the DB is also
correct. Eclipse doesn't issue any warning while running the project.

Here is the server-side code

<code>
package gwt.hom.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import gwt.hom.client.CInterface;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class CInterfaceImpl extends RemoteServiceServlet implements
CInterface {

public static ArrayList<String> list1Elem, list2Elem;

@Override
public ArrayList<String> getInfo1() {
list1Elem = new ArrayList<String>();

Window.alert("I am here!");

try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
String url = "jdbc:mysql://127.0.0.1:3306/localdb";
Connection conn = DriverManager.getConnection(url, "kicsyromy",
"zomfg");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select Nume from Tabel1 order by
Nume");
while (rs.next()) {
list1Elem.add(rs.getString("Nume"));
Window.alert(" " + list1Elem);
}

conn.close();

} catch (SQLException ex) {
Window.alert(ex.getMessage());
}
return list1Elem;
}

@Override
public ArrayList<String> getInfo2() {
list2Elem = new ArrayList<String>();


try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
String url = "jdbc:mysql://127.0.0.1:3306/localdb";
Connection conn = DriverManager.getConnection(url, "kicsyromy",
"zomfg");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select Nume from Tabel2 order by
Nume");
while (rs.next()) {
list2Elem.add(rs.getString("Nume"));
Window.alert(" " + list2Elem);
}

conn.close();

} catch (SQLException ex) {
Window.alert(ex.getMessage());
}
return list2Elem;
}
}
</code>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment