O q há de errado com esta classe?

2 respostas
S
import java.sql.*;

/**
* <p>Title: MySQL Database Connection </p>
* <p>Description: This was the first attempt to connect to a MySQL database</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author Kiwiburger
* @version 1.0
*/
public class msql_test {

Connection connection;
private void displaySQLErrors(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
public void connectToDB() {
try {
//change the user name and password to yours
String userName = "username";
String password = "password";
//change the database name to your db
String url = "jdbc:mysql://localhost/databasename";
Class.forName ("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}catch(SQLException e) {
displaySQLErrors(e);
}catch(ClassNotFoundException cnfe){
System.err.println("Unable to find and load driver: " + cnfe.getException());
System.exit(1);
}
}
public void executeSQL() {
try {
Statement statement = connection.createStatement();
//change the table name to your table
ResultSet rs = statement.executeQuery("select * from tablename;");

//I have multiple strings in the table I was using for the example...just
//add or change as you need them
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) +
" " + rs.getString(4) + " " + rs.getString(5)
+ " " + rs.getString(6) + " " + rs.getString(7)
+ " " + rs.getString(8) + " " + rs.getString(9)
+ " " + rs.getString(10) + " " + rs.getString(11)
+ " " + rs.getString(12) + " " + rs.getString(13)
+ " " + rs.getString(14) + " " + rs.getString(15));
}
rs.close();
statement.close();
connection.close();
}catch(SQLException e) {
displaySQLErrors(e);
}
}

public static void main(String[] args) {
      msql_test mysql_test = new msql_test();
      mysql_test.connectToDB();
      mysql_test.executeSQL();
}
}

tirei isso de um tutorial, mas n esta funcionando … =/
ele he compilado normalmente mas quando tento executa-lo da o erro:

Exception in thread “main” java.lang.NoClassDefFoundError: msql_test/java

2 Respostas

Rafael_Steil

Voce provavelmente esta digitando

java msql_test.java

quando deveria fazer

java msql_test

Outro detalhe: eh recomendavel que o nome da classe comece com uma letra maiuscula e sem underlines… ou seja: MsqlTest.

Rafael

S

Aew obrigado ! funcionou … acho q eu tava digitando errado mesmo … ^^

Criado 21 de maio de 2004
Ultima resposta 24 de mai. de 2004
Respostas 2
Participantes 2