Difference between revisions of "Connecting to Mysql"
From MyWiki
| (2 intermediate revisions 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, | + | con = DriverManager.getConnection(url,userName,passWord); |
| − | sql = " | + | sql = "select username, street from <table>"; /// Example |
| − | + | ||
stmt = con.createStatement(); | stmt = con.createStatement(); | ||
rs = stmt.executeQuery(sql); | rs = stmt.executeQuery(sql); | ||
| Line 22: | Line 21: | ||
while ( rs.next()) | 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 | |
</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