Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Properties;
- import java.io.*;
- public class DBMysql
- {
- // The JDBC Connector Class.
- private static final String dbClassName = "com.mysql.jdbc.Driver";
- // Connection string. emotherearth is the database the program
- // is connecting to. You can include user and password after this
- // by adding (say) ?user=paulr&password=paulr. Not recommended!
- private static final String CONNECTION =
- "jdbc:mysql://172.30.2.116/gestio";
- public static void main(String[] args)
- throws ClassNotFoundException,SQLException {
- // Class.forName(xxx) loads the jdbc classes and
- // creates a drivermanager class factory
- Class.forName(dbClassName);
- // Properties for user and password. Here the user and password are both 'paulr'
- Properties p = new Properties();
- p.put("user","guillem");
- p.put("password","guillem");
- // Now try to connect
- Connection c = DriverManager.getConnection(CONNECTION,p);
- exempleSelect(c);
- c.close();
- }
- private static void exempleSelect(Connection conn){
- try {
- Statement stmt = conn.createStatement();
- String sql = "select id_municipi,descripcio from MUNICIPI";
- ResultSet rs = stmt.executeQuery(sql);
- //STEP 5: Extract data from result set
- while(rs.next()){
- //Retrieve by column name
- int id_municipi = rs.getInt("id_municipi");
- String descripcio = rs.getString("descripcio");
- //Display values
- System.out.print("id_municipi: " + id_municipi);
- System.out.println(", descripcio: " + descripcio);
- }
- rs.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- System.out.println("error sql");
- se.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement