Skip to content

Archive

Category: Programming

This should be a very simple problem, but finding the solution does not always jump out at you based on the tutorials out there.

When doing a JDBC connection for SQL Server you may have the following line of code:


String sqlServerConnectionString = "jdbc:sqlserver://10.0.0.233:1433; 
databaseName=mydatabasename;user=sa;password=mypassword";
Connection con = DriverManager.getConnection(sqlServerConnectionString);

Then you go to run it and run into the following:
java.sql.SQLException: No suitable driver

Actually, the problem is very simple. Earlier on, you may have forgotten this:


try
{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e)
{
    System.out.println(e);
}
try
{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e)
{
    System.out.println(e);
}

//Now run the code you originally ran:
String sqlServerConnectionString = "jdbc:sqlserver://10.0.0.233:1433;
databaseName=mydatabasename;user=sa;password=mypassword";
Connection con = DriverManager.getConnection(sqlServerConnectionString);

That should either solve your problem or at least give you a hint of what to do next.

Here it is 2009 and we have finally confronted this problem. We had some users trying to upload Office 2007 files into the Jive application and the uploads were not sticking. After researching the problem, I determined the problem had to do with the content types being named something larger than the varchar2(50) in the database was able to handle so I had to increase the size of the field. In doing the research, I came across a good posting which lists all of the content-types used by Microsoft Office 2007 applications. This is the URL:

http://www.bram.us/2007/05/25/office-2007-mime-types-for-iis/

So if you need to know all of the content-types for some odd reason, there you go.

This one stumped me for some time. I was trying to simply install the Sun version of the JDK onto a VMWare instance of Red Hat ES4 and I kept coming upon this error. I initially figured it must be a dependency issue and I tried all sorts of things to get it to work. Later in the day, I tried downloading the JDK without the Enterprise Edition version of Java. This time when I ran the executable to install the JDK it worked perfectly. Evidently, the JDK with the EE attached does not want to install on Red Hat ES4. Hopefully, you won’t need it and the JDK without the EE serves you fine. What a nasty hidden bug.

I was working on a wiki for one of the Quest Communities called http://wiki.activeroles.quest.com. This wiki is using Media Wiki which is an Open Source wiki built on PHP. In customizing the look and feel to this wiki to make it look like its parent community site, I ran into an issue with the pull down menus created by the Quest web development team. The pull down menus worked great. However, when the menus dropped down, certain of the MediaWiki components insisted on being in front of the pull down menus.

In resolving the issue, I looked at both the CSS and the HTML layout of the media wiki components which were nothing more than div tags. I tried to solve the problems by modifying absolute vs relative positioning, modifying the z-index on the various components, and I was really tested on finding the solution. Then I found it.

The CSS attribute of the dominant div tags included overflow:visible; as attributes. For some reason, this attribute was forcing the div tag to be on the top layer.

/* overflow:visible; */

Problem solved.

Hibernate works very well in Swing Applications. This page is intended to help you get started in implementing Hibernate with Swing. This particular example is set up in a database environment using Oracle 10g. However, this setup will work perfectly in other database enviroments by simply modifying the database connection information.

1. Download the latest Version
Hibernate can be found http://www.hibernate.org/6.html. After you download it, make certain you not only place hibernate.jar in you classpath, you may want to include all of the jar files that are found in the lib directory of the extracted files. While many may not be used in your application, some of them are likely to result in runtime errors if not included in the classpath.

2. Create the hibernate.cfg.xml file.
This is the primary configuration file that should be placed at the root of your source code.

<?xml version=“1.0″ encoding=“UTF-8″?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>
<session-factory>
<property name=“hibernate.connection.driver_class”>oracle.jdbc.driver.OracleDriver </property>
<property name=“hibernate.connection.url”> jdbc:oracle:thin:@servername.domainname.com:1521:dbname</property>
<property name=“hibernate.connection.username”>username</property>
<property name=“hibernate.connection.password”>password</property>
<property name=“hibernate.connection.pool_size”>10</property>
<property name=“show_sql”>true</property>
<property name=“dialect”>org.hibernate.dialect.Oracle10gDialect</property>
<property name=“connection.autocommit”>true</property>
<!– Mapping files –>
<mapping resource=“Entry.hbm.xml”/>
</session-factory>
</hibernate-configuration>

You may need to modify the dialect to match your current version of Oracle. Other available dialects for the Oracle database include OracleDialect (deprecated), Oracle8iDialect, and Oracle9iDialect. It is also important that you set the autocommit property to true if you are not using exlicit transactions in your application. Otherwise, you may get frustrated as you try to look at the database and see that your changes have not been commited to the database.

3. Create a class to match a given database table.
This example called Entry.java matches a database table called Entry.

package com.utilities.data;

public class Entry
{
private int entryID;
private int userID;
private String title = “”;
private long creationDate;

public Entry(){ }

public void setEntryID(int entryID) { this.entryID=entryID; }
public void setUserID(int userID) { this.userID=userID; }
public void setTitle(String title) { this.title=title; }
public void setCreationDate(long creationDate) { this.creationDate = creationDate; }

public int getEntryID() { return this.entryID; }
public int getUserID() { return this.userID; }
public String getTitle() { return this.title; }
public long getCreationDate() { return this.creationDate; }
}

The corresponding table creation and sequence creation commands are given here as well.

CREATE SEQUENCE Entry_seq MINVALUE 1 START WITH 1 INCREMENT BY 1;

CREATE TABLE ENTRY
(
EntryID Number Primary Key,
UserID Number Not Null,
Title Varchar2(255),
CreationDate Number
);

With this example you can will are shown using int, String, and long datatypes in Hibernate. In this particular application, the architects choose to use the Date.getTime() functionality to store date values.

4. Create the Mapping Resource File.

This particular file is called Entry.hbm.xml. It is stored in the root directory right next to the hibernate.cfg.xml file and is used to bridge the java object values to the database. In many applications, it is prefered to place this file right next to Entry.java. However, it appears that in Swing applications, Hibernate has difficulty finding it unless it is in the root directory.

<?xml version=“1.0″ encoding=“UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping package=“com.utilities.data”>
<class name=“Entry” table=“Entry”>
<id name=“entryID” column=“EntryID” type=“int”>
<generator class=“sequence”>
<param name=“sequence”>Entry_seq</param>
</generator>
</id>
<property name=“userID” type=“int” />
<property name=“title” type=“string” length=“255″ />
<property name=“creationDate” type=“long” />
</class>
</hibernate-mapping>

5. Populate the class called Entry as you would any other java class.

6. Save the data to the database
From within your DAO class make the following calls. Please pay attention to the following line:

configuration = new Configuration().configure(”/hibernate.cfg.xml”);

In some applications, it is enough to simply call configuration = new Configuration()

Unfortuneately, my swing application required the additional configure method to be called in order to find the configuration file.

package com.utilities.data;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;

public class ImportToolDAO
{

private Configuration configuration;
private SessionFactory factory;
private static ImportToolDAO importToolDAO;

private ImportToolDAO()
{
configuration
= new Configuration().configure(“/hibernate.cfg.xml”);
factory
= configuration.buildSessionFactory();
}

public static ImportToolDAO getInstance()
{
if(importToolDAO==null)
{
importToolDAO
= new ImportToolDAO();
}
return importToolDAO;
}

public void saveEntry(Entry entry)
{
Session session = factory.openSession();
session
.save(entry);
session
.flush();
session
.close();
}
}

7. Run the Application. If you followed everything step by step, it just might work for you the first time. Go check your database and be prepared for a smile.

Ran across this error while building the Swing application with Hibernate. Originally, the Entry.hbm.xml file was placed deep in the directory structure next to the java class called Entry.java. Several ways of naming the xml file where tried. The only solution that worked in this case was to move the file outside of the classpath structure and place it in the root directory next to the hibernate.cfg.xml file.

Ran across this error while setting up an application using Hibernate. After googling it some, I came across a thread (in Spanish) referencing the need to include the cglib-2.1.3.jar. That may have fixed the problem; nevertheless, the problem had to do with not including all of the hibernate jar files found in the hibernate-3-X-X.ga.zip file. There are more jar files needed than just hibernate.jar. Make certain you include all of jar files that are in the lib folder of the downloaded uncompressed hibernate file. If all of the files are included, you should not run into this error.

Was trying to get setup with Hibernate and ran into the following error and stack trace:

INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 26, 2007 11:40:40 AM org.hibernate.connection.UserSuppliedConnectionProvider configure
WARNING: No connection properties specified - the user must supply JDBC connections
Exception in thread "AWT-EventQueue-0" org.hibernate.HibernateException: Hibernate Dialect 
must be explicitly set        
    at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)        
    at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)        
    at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)        
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)        
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)        
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)

This error occured as Hibernate was unable to locate the hibernate.cfg.xml file. To fix this, I had to change the way of calling the Hibernate Configuration class. For some reason, in my web based applications, the first example of calling the class worked perfectly. It did not work in my Swing application so I changed to the the second version below:

Version 1:
Configuration configuration = new org.hibernate.cfg.Configuration();

Version 2:
org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration().configure(”/hibernate.cfg.xml”);

Ran into this error tonight while copying tables from one database into another.

I was using the mysqldump command as follows:

mysqldump -h localhost -u username -p –add-drop-table wildernessgear wp_31_term_relationship /home/bnettles/wp_31_term_relationship.sql

This command was supposed to export just the one table wp_31_term_relationship. I had already exported the 30 previous tables in the database. All of a sudden this one was causing a problem with the following error message.

mysqldump: Got error: 1146: Table ‘wildernessgear.wp_31_term_relationship ‘ doesn’t exist when doing LOCK TABLES.

I searched the internet for a while and wasted a half an hour not being able to figure out how to unlock the tables. Then I discovered the real problem. The table did not exist. There was a typo. It was supposed to be wp_31_term_relationships with an “s” at the end, not wp_31_term_relationship.

Looks like Mysql is using the wrong error message in this case.