[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] mysql i java



Modifiera nedanstående kodstump så att den passar din databas(ändra lösenord, db-namn, etc.) och provkör det hela. Får du inte det till att fungera är troligtvis inte Connector/J korrekt installerad. Jag har lagt .jar-filen i $JAVA_HOME/jre/lib/ext och mina JDBC-apps funkar jättebra.
Hoppas det funkar. Annars får du ropa till så meckar vi lite till :-)


//Daniel

dbTest.java:

import java.sql.*;

public class dbTest {


public static void main(String[] args) throws SQLException {
Connection con = null;
try {
System.out.println("Trying to connect...");
con = getConnection();
System.out.println("Connection made!");
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT customer_id, customer_name,customer_email FROM customers");
while (rs.next()) {
// retrieve and print the values for the current row
int i = rs.getInt("customer_id");
String s = rs.getString("customer_name");
String f = rs.getString("customer_email");
System.out.println("ROW = " + i + " " + s + " " + f);
}
} finally {
if (con != null) {System.out.println("Closing connection.");con.close();}
}
System.out.println("Over and out.");
}


  private static Connection getConnection() throws SQLException {
    String  dbDriver = "com.mysql.jdbc.Driver",
            dbHost = "localhost",
            dbPort = "3306",
            dbName = "customerDB",
            dbUser = "un",
            dbPass = "pw",
            dbURL = "jdbc:mysql://"+dbHost+":"+dbPort+"/"+dbName;
    Connection con = null;

    try {
      Class.forName(dbDriver).newInstance();
    } catch(Exception e) {
      throw new SQLException("Class not found: "+e.getMessage());
    }
    con = DriverManager.getConnection(dbURL,dbUser,dbPass);
    return con;
  }
}

--
Excuse me, I believe you have my stapler...?


Allan Nielsen wrote:
Hej
Nogle der har prøvet at bruge en mysql database i java, jeg har prøvet
at følge guiden på:
http://www.thep.lu.se/~nicklas/base2/prototype/mysql.html
uden held, jeg tror ikke jeg får installeret pakken fra mysqls hjemme
side rigtig.

Hvis der er nogle som har nogle eksempler/erfaringer/forslag ville dette
være en stor hjælp

MVH
Allan Nielsen





 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *