Difference between revisions of "Connecting to Mysql"
From MyWiki
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
String url = "jdbc:mysql://localhost<dbname>"; | String url = "jdbc:mysql://localhost<dbname>"; | ||
− | Class.forName("com.mysql.jdbc.Driver").newInstance(); | + | Class.forName("com.mysql.jdbc.Driver").newInstance(); /// Starting here must be in a try catch |
con = DriverManager.getConnection(url,userName,passWord); | con = DriverManager.getConnection(url,userName,passWord); | ||
sql = "select username, street from <table>"; /// Example | sql = "select username, street from <table>"; /// Example | ||
Line 25: | Line 25: | ||
System.out.println(Username + " " +Street); | System.out.println(Username + " " +Street); | ||
} | } | ||
− | + | ///// note the above has to be in a try - catch section | |
</source> | </source> |
Latest revision as of 12:44, 24 February 2016
import java.sql.*; { Connection con = null; Statement stmt = null; ResultSet rs = null; String userName = "<username>"; String passWord = "<password>"; String sql = ""; String url = "jdbc:mysql://localhost<dbname>"; Class.forName("com.mysql.jdbc.Driver").newInstance(); /// Starting here must be in a try catch con = DriverManager.getConnection(url,userName,passWord); sql = "select username, street from <table>"; /// Example stmt = con.createStatement(); rs = stmt.executeQuery(sql); while ( rs.next()) { String Username = rs.getString("username"); String Street = rs.getString("street"); System.out.println(Username + " " +Street); } ///// note the above has to be in a try - catch section