Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Properties;
- import java.sql.DriverManager;
- public class Main {
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- //Load MySQL driver
- String driver = "com.mysql.jdbc.Driver";
- Class.forName(driver);
- Connection con = getConnection();
- viewTable(con, "SpelRsi");
- }
- public static void viewTable(Connection con, String dbName)
- throws SQLException {
- Statement stmt = null;
- String query = "SELECT beskrivning FROM Produkt";
- try {
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()) {
- String beskrivning = rs.getString("beskrivning");
- System.out.println(beskrivning + "\t");
- }
- }finally {
- if (stmt != null) { stmt.close(); }
- }
- }
- public static Connection getConnection() throws SQLException {
- Connection con = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", "root");
- connectionProps.put("password", "Nicock");
- con = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/SpelRsi",
- connectionProps);
- System.out.println("Connected to database");
- return con;
- }
- }
Add Comment
Please, Sign In to add comment