Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.lang.*;
- public class main{
- private static void request(){
- String url = "jdbc:mysql://myadress:3306/mydatabase";
- String user = "user";
- String passwd = "passwd";
- Connection conn = null;
- try{
- /* Initializing the connection */
- conn = DriverManager.getConnection(url, user, passwd);
- Statement statement = conn.createStatement();
- ResultSet resultset = statement.executeQuery(/* MY SQL reqquest */);
- while(resultset.next()){
- System.out.println(resultset.getString(/* THE COLUMN AND ROW I WANTED IN MY REQUEST */));
- }
- }catch(SQLException e){
- System.out.println("SQL connection error: " + e.getMessage());
- }finally {
- if(conn != null){
- try{
- /* CLosing connection */
- conn.close();
- }catch (SQLException e){
- System.out.println("Error while closing the connection: " + e.getMessage());
- }
- }
- }
- }
- public static void main(String[] args) {
- try{ // Loading the MySQL Connector/J driver
- Class.forName("com.mysql.jdbc.Driver");
- }catch(ClassNotFoundException e){
- System.out.println("Error while loading the Driver: " + e.getMessage());
- }
- request();
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public void conectarBDMySQL (String user, String password,
- String ip, String port, String cat)
- {
- if (conexionMySQL == null)
- {
- String urlConexionMySQL = "";
- if (cat!= "")
- urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port+ "/" + cat;
- else
- urlConexionMySQL = "jdbc:mysql://" + ip + ":" + port;
- if (user!= "" & password!= "" & ip != "" & port!= "")
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conexionMySQL = DriverManager.getConnection(urlConexionMySQL,
- user, password);
- }
- catch (ClassNotFoundException e)
- {
- Toast.makeText(getApplicationContext(),
- "Error: " + e.getMessage(),
- Toast.LENGTH_SHORT).show();
- }
- catch (SQLException e)
- {
- Toast.makeText(getApplicationContext(),
- "Error: " + e.getMessage(),
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment