Tech-Today
How to connect MySql or PostgreSql Server from Java using JDBC Driver
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");
-
Jboss Datasource Configuration Settings
The following are sample configurations for different database (it's in the file standalone.xml subsystem=datasources): H2, postgresql, mysql <datasources> <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true"...
-
Jdbc Provider Setting In Java For Postgresql, Mysql And Oracle
Below are the summary of connection settings for differenct jdbc provider: Database Connection Url Driver Class Hibernate Dialect Cache Provider Class PostgreSQL jdbc:postgresql://[host]:[port]/[db] org.postgresql.Driver org.hibernate.dialect.PostgreSQLDialect...
-
Connect To Oracle Database In Java Language
This code connects to an oracle database using java programming language. This to remember: 1.) You must add ojdbc14.jar to the project's build path. 2.) Oracle's driver name: oracle.jdbc.driver.OracleDriver 3.) Connection string: jdbc:oracle:client:@server:port:database...
-
How To Create And Call A Database Routine (stored Procedure/function) Using A Jdbc Driver In Java
How to create and call a database routine (stored procedure/function) using a jdbc driver in java Stored procedure/function is a routine that is defined and executed in the database itself. Minimizing the time of execution, since the code is already residing/compiled...
-
Sqlce Get Last Insert Id, Convert Sqldecimal To Int32
Platform: Windows Mobile, SqlCE2.0 Problem: You want to get the id of the last query you insert. Solution: Using the @@identity you have to execute your queries like this: SqlCeConnection conn = null; try { conn = new SqlCeConnection(GetConnection());...
Tech-Today