Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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();
}
}
}
}
}
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!