Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class DBConnect {
- public static void main(String[] args) throws Exception
- {
- // load driver, for JDBC 3 and earlier
- Class.forName("com.mysql.jdbc.Driver");
- // make the connection using the supplied credentials
- Connection connect =
- DriverManager.getConnection("jdbc:mysql://localhost:8889/java_demo","root","root");
- // execute a statement and get the resultSet
- Statement st = connect.createStatement();
- ResultSet resultSet = st.executeQuery("SELECT * FROM messages");
- // iterate through the resultSet and print them out
- while (resultSet.next())
- {
- System.out.println(resultSet.getInt("id") + " "
- + resultSet.getString("user") + " : "
- + resultSet.getString("message"));
- }
- // clean up for earlier JVM's
- connect.close();
- st.close();
- resultSet.close();
- }
- }
Add Comment
Please, Sign In to add comment