no

How to Connect Mysql or Postgresql Server Using Java

How to connect MySql or PostgreSql Server from Java using JDBC Driver I think the best answer to this is through coding: private static ...

How to connect MySql or PostgreSql Server from Java using JDBC Driver

I think the best answer to this is through coding:

private static Connection getConnection() {
 Connection conn = null;
 try {
  Class.forName("com.mysql.jdbc.Driver");
  //conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/anime", "ipieluser", "ipielpassword"); //postgresql
  conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/anime", "ipieluser", "ipielpassword"); //mysql
 } catch(Exception e) {
  e.printStackTrace();
 } finally {
 try {
   if(!conn.isClosed())
    conn.close();
  } catch(SQLException e1) { }
 }
}

To use the method and execute an sql in the server:

try {
 Connection conn = getConnection();
 Statement st = conn.createStatement();
 
 st = conn.createStatement();
 ResultSet rs = st.executeQuery("SELECT * FROM localTable");
 ResultSetMetaData rsMetaData = rs.getMetaData();
 int colCount = rsMetaData.getColumnCount();
 
 //get and display the number of columns
 System.out.println("resultSet MetaData column Count=" + colCount); 
 
 //display the column names
 for(int i = 1; i < colCount; i++) {
  System.out.print(rsMetaData.getColumnName(i) + " ");
 }
 
 st.close();
 conn.close();
} catch(Exception e) {
 System.out.println(e.getMessage());
}

Things to remember:
-DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/anime", "ipieluser", "ipielpassword"); //mysql
=DriverManager.getConnection("jdbc:database://host:port/databasename", "username", "password");

Related

java-persistence 2443081656760566885

Post a Comment Default Comments

item