Difference between revisions of "Connecting to Mysql"
From MyWiki
(Created page with "<source lang="java"> import.java.sql.*; { Connection con = null; Statement stmt = null; ResultSet rs = null; String userName = ""; String passWord = ""; String url = "jdbc:m...") |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<source lang="java"> | <source lang="java"> | ||
− | import | + | import java.sql.*; |
{ | { | ||
Line 7: | Line 7: | ||
Statement stmt = null; | Statement stmt = null; | ||
ResultSet rs = null; | ResultSet rs = null; | ||
− | String userName = ""; | + | |
− | String passWord = ""; | + | String userName = "<username>"; |
− | String url = "jdbc:mysql://localhost<dbname>" | + | 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 | ||
</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