| View previous topic :: View next topic |
| Author |
Message |
Whisky Guest
|
Posted: Fri Nov 21, 2003 12:27 am Post subject: Problem Java MySQL |
|
|
I'm trying to connect a simple javaprogram to a MySQL-database.
Do someone have a codeexample of a working program thats does a simple
selectquery and then print the answer on the screen. Lets say it should
print the famous word - HelloWorld. It should be "from the beginning".
Thanks in advance.
|
|
| Back to top |
|
 |
Christophe Vanfleteren Guest
|
Posted: Fri Nov 21, 2003 12:37 am Post subject: Re: Problem Java MySQL |
|
|
Whisky wrote:
| Quote: | I'm trying to connect a simple javaprogram to a MySQL-database.
Do someone have a codeexample of a working program thats does a simple
selectquery and then print the answer on the screen. Lets say it should
print the famous word - HelloWorld. It should be "from the beginning".
Thanks in advance.
|
http://java.sun.com/docs/books/tutorial/jdbc/
--
Regards,
Christophe Vanfleteren
|
|
| Back to top |
|
 |
Tanel Guest
|
|
| Back to top |
|
 |
Whisky Guest
|
Posted: Fri Nov 21, 2003 7:36 pm Post subject: Re: Problem Java MySQL |
|
|
forgot to tell that it is on a stand-alone machine, dvs local host.
=)
/E
"Tanel" <tanel.lebedev (AT) d-codex (DOT) com> skrev i meddelandet
news:3fbe135e$1_1 (AT) news (DOT) estpak.ee...
|
|
| Back to top |
|
 |
nigel salt Guest
|
Posted: Sat Nov 22, 2003 8:54 am Post subject: Re: Problem Java MySQL |
|
|
I think this is about as simple as it gets
public class jdbcTableDump {
public static void main(String args[]) {
String url = "jdbc:mysql://127.0.0.1:3306/nigel";
String query = "select * from comics";
String username="nigel";
String password="nigel";
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, username, password);
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
// column names start at 1
for (int i=1;i<=numberOfColumns;i++) {
System.out.print(rsmd.getColumnName(i)+"t");
}
System.out.println("");
while (rs.next()) {
for (int i=1;i<=numberOfColumns;i++) {
System.out.print(rs.getString(i)+"t");
}
System.out.println("");
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
"Whisky"
| Quote: | I'm trying to connect a simple javaprogram to a MySQL-database.
Do someone have a codeexample of a working program thats does a simple
selectquery and then print the answer on the screen. Lets say it should
print the famous word - HelloWorld. It should be "from the beginning".
Thanks in advance.
|
|
|
| Back to top |
|
 |
|