Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC using MSQL works

 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the beginning, I was modifying the classpath in the autoexec.bat file thinking this would search for the MsqlDriver. This hasn't worked. Secondly I was using WinZip to open the jar file which seems to be a mistake. What has worked is to use the jar utility to open the jar file and to copy the driver to the directory indicated by the package and then importing this package into you application. The code below was shamelessly ripped off and does work. There may be other solutions but this one has worked for me.
import java.sql.*;
import com.imaginary.sql.msql.*;

public class Connect
{
public static void main ( String args[] )
{
Connection mSQL = null;
try
{
new MsqlDriver();
String url = "jdbc:msql:///StockMarket";
Connection mSQLcon = DriverManager.getConnection ( url );
Statement stmt = mSQLcon.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT * FROM Stock");
while( rs.next())
{
System.out.println( rs.getString(1));
}
}
catch ( SQLException sql )
{
sql.printStackTrace();
}
finally
{
if ( mSQL != null )
{
try
{
mSQL.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic