Connecting MySQL workbench 6.0 to SpringMVC
I'm trying to connect MySQL workbench to my eclipse, I'm planning to use
SpringMVC for the development but i tried first to connect the Test
project to the work bench but it seems doesn't work for me.
Here is the StackTrace:
javax.naming.NoInitialContextException: Need to specify class name in
environment or system property, or as an applet parameter, or in an
application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at
com.database.DatabaseCommunication.getDataSource(DatabaseCommunication.java:33)
at com.database.StudentDAO.main(StudentDAO.java:71)
Exception in thread "main" java.lang.NullPointerException
at com.database.StudentDAO.main(StudentDAO.java:84)
Database connection i tried:
public final class DatabaseCommunication {
public static DatabaseCommunication getInstance() {
return DatabaseCommunicationHolder.INSTANCE;
}
private DataSource dataSource;
public synchronized DataSource getDataSource() throws NamingException{
if( dataSource == null ) {
InitialContext cxt = new InitialContext();
dataSource = ( DataSource ) cxt.lookup(
"java:/comp/env/jdbc/THESIS" );
}
return dataSource;
}
private static class DatabaseCommunicationHolder {
public static final DatabaseCommunication INSTANCE = new
DatabaseCommunication();
}
}
my context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/THESIS" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="root"
url="jdbc:mysql://localhost:3306/thesis_test_project"
driverClassName="com.mysql.jdbc.Driver" />
</Context>
This is where i tried the code:
public static void main( String args[] ) {
DatabaseCommunication db = DatabaseCommunication.getInstance();
ArrayList<String> studentName = null;
try {
studentName = new StudentDAO( db.getDataSource() ).getName( 1 );
}
catch( SQLException e ) {
e.printStackTrace();
}
catch( NamingException e ) {
e.printStackTrace();
}
System.out.println( studentName.get( 0 ) );
}
And lastly the query which is on the same class as the main
public ArrayList getName( int id ) throws SQLException {
Connection connection = dataSource.getConnection();
PreparedStatement getNameStatement =
connection.prepareStatement( "SELECT * from student
where id = ?" );
getNameStatement.setInt( 1, id );
ResultSet rs = getNameStatement.executeQuery();
ArrayList<String> name = new ArrayList<String>();
while( rs.next() )
{
name.add( rs.getString( "name" ) );
}
rs.close();
getNameStatement.close();
connection.close();
return name;
}
I'm just new to setting up databases, the code above is just the result of
my search. I just have a minimum knowledge of what i did above. Can you
tell me what i did wrong in detail?
By the way the JARs that i import at WebContent/WEB-INF/lib if it matters
JARs
mysql-connector-java-5.1.26.jar
spring-beans-4.0.0.M2.jar
spring-context-4.0.0.M2.jar
spring-core-4.0.0.M2.jar
spring-extension-4.0.0.M2.jar
spring-jdbc-4.0.0.M2.jar
spring-web-4.0.0.M2.jar
spring-webmvc-4.0.0.M2.jar
No comments:
Post a Comment