import java.sql.*;
public class TestMysql
{
public static void main(String args[])
{
Connection conn = null;
try {
/* Test loading driver */
String driver = "com.mysql.jdbc.Driver";
System.out.println( "=> loading driver:" );
Class.forName( driver );
System.out.println( "OK" );
/* Test the connection */
String url = "jdbc:mysql://localhost:11819/test";
System.out.println( "=> connecting:" );
conn = DriverManager.getConnection( url, "test", "test" );
System.out.println( "OK" );
printQueryResults(conn, "SELECT VERSION()");
if(conn != null)
conn.close();
}
catch( Exception x ) {
x.printStackTrace();
}
}
public static void printQueryResults(Connection conn, String SQLquery)
throws Exception {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(SQLquery);
int columns = rs.getMetaData().getColumnCount();
System.out.println("------------------------");
System.out.println();
while (rs.next()) {
for (int i = 1; i <= columns; i++) {
System.out.println(rs.getString(i));
}
System.out.println();
}
rs.close();
stmt.close();
System.out.println("------------------------");
System.out.flush();
Thread.sleep(100); // wait for System.out to finish flush
}
}
Tips and scripts relating to database administration. Mainly SQL Server but some MySQL and every now and again something different. Posts are brief and to the point.It's as much a place to make notes for my own use as anything else.
Thursday, 6 September 2007
Java TestMySql
Jave Paths
export CLASSPATH=.:/home/phil.lewin/java/packages/mysql-connector-java-5.0.6-bin.jar:
javac TestMysql.java
/usr/java/jdk1.6.0/bin/java TestMysql
javac TestMysql.java
/usr/java/jdk1.6.0/bin/java TestMysql
Monday, 3 September 2007
Subscribe to:
Comments (Atom)