Added
Jumat, 28 Mei 2010
, Label:
Java SE
One program that is attractive for testing through the console before will start using the IDE is a program connecting to the database using JDBC. Why this is interesting because there are some distinctive aspects of Java contained therein.
We can try to connect to the MySQL database through Java. To do this, please do the tutorial as follows:
Save with name TestMysql.java, then open Console(ALT + F2), Compile javac TestMysql.java If no error message, continue to run the program java TestMysql, and this is the Screen Out :

*Note:
The above program uses a local MySQL database server (localhost) with the test database, guest user name and password blank. These settings are the default settings and there is always at MySQL. If you want to modify, adjust the condition of your MySQL settings.
We can try to connect to the MySQL database through Java. To do this, please do the tutorial as follows:
- Installing the MySQL database server. To install the MySQL database on OpenSUSE please refer to the guidelines http://www.vavai.com/index.php?/categories/7-Migrasi-Database
- Running service MySQL with YAST | SYSTEM | SYSTEM SERVICE (RUNLEVEl)
- Download the MySQL Connector J through http://www.mysql.com/products/connector/j/ address. Grab the latest version. At the time this guide was written, the latest version is mysql-connector-java-5.1.5
- Extract the downloaded file and grab the file mysql-connector-java-5.1.5-bin.jar. Customize with downloadable versions of MySQL Connector.
- Copy these jar files to the location of the library. For compatibility with my other guides, please copy it to the folder / opt/jdk1.6.0/lib
- Open konsole and type the command..
- export CLASSPATH =.: / opt/jdk1.6.0/lib/mysql-connector
- For convenience, the command above can enter into the files. Bash_profile in their respective home folders
- Open Netbeans and type the following program code :
public class TestMysql { public static void main(String args)
try {
/* Test loading driver */
String driver = "com.mysql.jdbc.Driver";
Class.forName( driver );
System.out.println( "OK" );/* Test the connection */
String url = "jdbc:mysql://localhost/test";
java.sql.DriverManager.getConnection( url, "guest", "");
System.out.println( "OK" );
}
catch( Exception x ) {
x.printStackTrace();
}}}
Save with name TestMysql.java, then open Console(ALT + F2), Compile javac TestMysql.java If no error message, continue to run the program java TestMysql, and this is the Screen Out :
*Note:
The above program uses a local MySQL database server (localhost) with the test database, guest user name and password blank. These settings are the default settings and there is always at MySQL. If you want to modify, adjust the condition of your MySQL settings.
- java: command not found, it is usually not correct PATH settings. Try a test by running the command echo $ PATH on the console, and whether the response indicates the location of the installed JDK
- TestMysql class is public, Should be declared in a file named TestMysql.java. Occurs if the filename is different from the name of the class. Remember that Java adheres case sensitive, so different from the testmysql TestMysql
- Cannot resolve symbol
- Exception in thread "main" java.lang.NoClassDefFoundError: TestMysql, usually occur because of running the class files with the extension. Java should TestMysql, not java or TestMysql.class TestMysql.java
More and more exercise, we will be more accustomed and more know what is stipulated in the existing error message.
By dejava with
0
komentar


0 komentar: