I'm creating java web application using maven. I added mySQL local database as a datasource and when I'm testing connection everything is ok however when I start the app and try to add a new record it doesn't work, I mean code is executing and app is working but there is no new data in database. I also cannot check if there is some data in database. There is no error or anything but other instructions not conneted to database work properly. In my web.xml I've got
<resource-ref>
<res-ref-name>jdbc/DSTestPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
In jetty.xml
<New id="DSTestPool" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DSTestPool</Arg>
<Arg>
<New class="org.apache.commons.dbcp2.BasicDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://localhost:3306/psi3</Set>
<Set name="username">root</Set>
<Set name="password"></Set>
<Set name="maxTotal">100</Set>
<Set name="maxIdle">30</Set>
<Set name="minIdle">10</Set>
</New>
</Arg>
</New>
and I'm creating user like this
public class UsersDAO {
private DataSource ds;
public UsersDAO(DataSource ds) {
this.ds = ds;
}
public void createUser(User user){
Connection con = null;
PreparedStatement pst = null;
try {
con = ds.getConnection("root","");
String sql="insert into users (Username,Password,Valid) values(?,?,?)";
pst = con.prepareStatement(sql);
pst.setString(1, user.getLogin());
pst.setString(2, user.getPassMD5());
pst.setInt(3,1);
pst.executeUpdate();
} catch (Exception e) {
System.out.println(e);
} finally {
DbTools.closeQuietly(pst, con);
}
}
}
my datasources.xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" hash="4078573267">
<data-source source="LOCAL" name="MySQL - psi3@localhost" uuid="c2a621c7-089c-4d40-9767-1387f9bfb343">
<driver-ref>mysql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://localhost:3306/psi3</jdbc-url>
<driver-properties>
<property name="zeroDateTimeBehavior" value="convertToNull" />
<property name="tinyInt1isBit" value="false" />
<property name="characterEncoding" value="utf8" />
<property name="characterSetResults" value="utf8" />
<property name="yearIsDateType" value="false" />
</driver-properties>
<libraries />
</data-source>
</component>
</project>
I've tried a few things but nothing seems to work, test shows that connection is ok and I do not know what to do with that. I wrote some apps in java but it's my first time with java web and I have no idea what to do with that so I will be grateful if someone can help me. I'm using IntelliJ IDEA 14.0.3.
Aucun commentaire:
Enregistrer un commentaire