•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,083 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,942 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2069 | Replies: 2 | Solved
![]() |
•
•
Join Date: Jul 2006
Posts: 107
Reputation:
Rep Power: 3
Solved Threads: 0
Hi,
I just installed MySQL on my computer and went though a decent MySQL tutorial. Now I need you guys help me out so I can get my java programs to connect to MySQL.
I am using the following code from a book to connect.
When I run the program I get the following error. Even afer googling all the lines I can't seem to figure out what the problem is. Any help will be appreciated.
I just installed MySQL on my computer and went though a decent MySQL tutorial. Now I need you guys help me out so I can get my java programs to connect to MySQL.
I am using the following code from a book to connect.
import java.sql.*;
public class Hello{
Connection connection;
private void displaySQLErrors(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLEXception: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
public Hello() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void connectToDB() {
try {
connection = DriverManager.getConnection(
"jdbc:mysql://localost/accounts?user=userName&password=passwd");
}
catch(SQLException e) {
displaySQLErrors(e);
}
}
public void executeSQL() {
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM acc_acc");
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
statement.close();
connection.close();
}
catch (SQLException e) {
displaySQLErrors(e);
}
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.connectToDB();
hello.executeSQL();
}
}When I run the program I get the following error. Even afer googling all the lines I can't seem to figure out what the problem is. Any help will be appreciated.
SQLException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.UnknownHostException
MESSAGE: localost
STACKTRACE:
java.net.UnknownHostException: localost
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1183)
at java.net.InetAddress.getAllByName0(InetAddress.java:1136)
at java.net.InetAddress.getAllByName0(InetAddress.java:1109)
at java.net.InetAddress.getAllByName(InetAddress.java:1072)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:137)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at Hello.connectToDB(Hello.java:25)
at Hello.main(Hello.java:55)
** END NESTED EXCEPTION **
Last packet sent to the server was 18 ms ago.
SQLEXception: 08S01
VendorError: 0
Exception in thread "main" java.lang.NullPointerException
at Hello.executeSQL(Hello.java:35)
at Hello.main(Hello.java:56) Last edited by ryy705 : Feb 2nd, 2007 at 4:17 pm.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,646
Reputation:
Rep Power: 18
Solved Threads: 191
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- MySQL++ query question (C++)
- Problems installing MySQL++ wrapper for C++ language (C++)
- jsp with mysql connection (JSP)
- C++ and MySQL connection (C++)
- Local Area Connection Question (Network Security)
- remote mysql connection (MySQL)
- modem connection question... (OS X)
Other Threads in the Java Forum
- Previous Thread: Super-power compare method? <?>
- Next Thread: ServletContextAttributeListener not working



Linear Mode