File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Spring - MySql Connectivity Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring - MySql Connectivity Error" Watch "Spring - MySql Connectivity Error" New topic
Author

Spring - MySql Connectivity Error

San Nan
Greenhorn

Joined: Jul 01, 2005
Posts: 22
Dear All,

I am Making a small application for Database Communication for spring with MySql.

The Source is :


1)
import javax.sql.*;

public interface datacon

{

public DataSource dbcon();

}

2)
import org.springframework.jdbc.core.*;

import org.springframework.jdbc.datasource.*;

import org.springframework.jdbc.object.*;

import org.springframework.jdbc.support.*;

import javax.sql.*;

public class dataconimpl implements datacon

{

private DataSource dataSource;

public void setDataSource(DataSource ds)

{

dataSource = ds;

}

public DataSource dbcon()

{

return dataSource;

}

}

3)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>sun.jdbc.odbc.JdbcOdbcDriver</value>
</property>
<property name="url">
<value>jdbc dbc:test</value>
</property>
<!--
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>sql</value>
</property>
-->
</bean>

<bean id="datacon" class="dataconimpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>

</beans>

4)
import java.io.*;

import javax.sql.*;

import java.sql.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

import org.springframework.jdbc.core.*;

import org.springframework.jdbc.datasource.*;

import org.springframework.jdbc.object.*;

import org.springframework.jdbc.support.*;

public class springservlet extends HttpServlet

{

public void doPost(HttpServletRequest req,HttpServletResponse resp)

throws ServletException,IOException

{
System.out.println("Sanju1");
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

System.out.println("Sanju2");
String a = req.getParameter("text1");

String b = req.getParameter("text2");
System.out.println("Sanju3");
String c = req.getParameter("combo1");

String d = req.getParameter("combo2");

try

{

System.out.println("Wait...");

Resource res = new ClassPathResource("datacon.xml");

BeanFactory factory = new XmlBeanFactory(res);

datacon bean1 = (datacon)factory.getBean("datacon");
System.out.println("Sanju4");

DataSource ds=bean1.dbcon();
System.out.println("Sanju5");

if(d.equals("add"))

{
System.out.println("Sanju6");

JdbcTemplate jt = new JdbcTemplate(ds);
System.out.println("Sanju7");

jt.execute("insert into table1 values('"+a+"','"+b+"') ");
System.out.println("Sanju8");

out.println("Record Added");
System.out.println("Sanju9");

}

if(d.equals("delete"))

{

JdbcTemplate jt = new JdbcTemplate(ds);

jt.execute("delete from table1 where name='"+a+"' ");

out.println("Record Deleted");

}

if(d.equals("find"))

{

List list1;

JdbcTemplate jt = new JdbcTemplate(ds);

list1=jt.queryForList("select * from table1 where name='"+a+"'");

Iterator i=list1.iterator();

while(i.hasNext())

{

Object ob = i.next();

out.println(ob.toString());

}

}

if(d.equals("update"))

{

if(c.equals("name"))

{

JdbcTemplate jt = new JdbcTemplate(ds);

jt.execute("update table1 set table1.place='"+b+"'where table1.name='"+a+"' ");

}

if(c.equals("place"))

{

JdbcTemplate jt = new JdbcTemplate(ds);

jt.execute("update table1 set table1.name='"+a+"'where table1.place='"+b+"' ");

}

out.println("Record Updated");

}

}

catch(Exception e1)

{System.out.println("Sorry "+e1);}

}

}


5)
<html>
<body bgcolor="pink">
<form method=post action="http://localhost:8080/Sanjay/springservlet">
Name : <input type=text name="text1"> <br><br>
Place: <input type=text name="text2"> <br><br>
Criterion :
<select name="combo1" size=1>
<option value="name">Name
<option value="place">Place
</select> <br><br>

<select name="combo2" size=1>
<option value="add">Add
<option value="delete">Remove
<option value="find">Find
<option value="update">Update
</select> <br><br>
<input type=submit>
</form>
</body>
</html>

================================================================
I have also created a table in test database ( table1)

I have created a DSN ( without password & checked ( Tested) ( tried with User DSN & also with System DSN)
===============================================================


I am Getting the Following Error :

Error while getting database metadata
org.springframework.jdbc.support.MetaDataAccessException: Error while getting connection; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC connection; nested exception is java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC connection; nested exception is java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6879)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7036)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3028)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:156)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:144)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:132)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:165)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:144)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:120)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:93)
at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:209)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:115)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:96)
at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:76)
at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:93)
at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:131)
at springservlet.doPost(springservlet.java:84)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invok
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC connection; nested exception is java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
Looks like a database connection problem, most likely due to your driver (just for kicks google your driver class name and see what they say about using it). Anyway, if you're using MySQL download their driver (I think it's called MySQL JDBC Connector) and use it instead.

Eman
Clodoaldo J�nior
Greenhorn

Joined: Nov 12, 2005
Posts: 3
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.

You must insert driver in classpath of its application. The exception show that the driver not specified.

[]'s ClodoaldoJR


UFPB - University Federal of Paraiba - Brazil (bachelor degree)
Erwin Bredford
Ranch Hand

Joined: Feb 11, 2003
Posts: 167
1. install the mysql odbc connector
2.use the com.mysql Driver instead the odbc driver

it should work


--One learns a lot during a lesson but seeing is not enough,you must do;knowing is not enough ,you must apply--<br />SCJP 1.4,SCBCD,SCEA part 1,OCM JEE Enterprise Architect.
manglesh jain
Greenhorn

Joined: Jul 08, 2004
Posts: 7
HI San,
I was also trying this example of spring.
if you found the answer, canyou please reply the answer. what do i need to set to run this example.


Manglesh Jain<br />S/W Engg.,Pune-India
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

For future posts. If you need to post code or xml, use the CODE tags instead so that it keeps the formatting of indentations. The code posted above is unreadable. The CODE tag can be added by clicking the CODE button that is below the Add Reply button. There are about ten buttons down there to make posts easier to read.

Thanks and good luck with your driver issue.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring - MySql Connectivity Error