Reputation: 77
In PHP, we simply create a file, e.g. connect.php
and then we include this file to each and every page we like:
In Java I created a separate file in the same project folder called connect.java
and tried to call it in other files so that those files get the database connection but no luck so far .
I tried last time but what amazed me is that some variables from the connect page I was not able to use them in my main program . can someone please help me . bellow is the code I use to connect to mysql and also I prinde some stuff
but I wanna make it a separate file which don’t print nothing just to connect and I just print all what I want in a different file
so if I would talk in a php term I wanna include it into other files where connectivity might be needed.
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loading success!");
String url = "jdbc:mysql://localhost:3306/saloon";
String name = "root";
String password = "";
try {
java.sql.Connection con = DriverManager.getConnection(url, name, password);
System.out.println("Connected.");
// pull data from the database
java.sql.Statement stmts = null;
String query = "select userid, username, name , address, hairstyle from saloonuser ";
stmts = con.createStatement();
ResultSet rs = stmts.executeQuery(query);
int li_row = 0;
while(rs.next()){
mysTable.setValueAt(rs.getString("username"),li_row,0);
mysTable.setValueAt(rs.getString("name"),li_row,1);
mysTable.setValueAt(rs.getString("address"),li_row,2);
mysTable.setValueAt(rs.getString("hairstyle"),li_row,3);
int userid = rs.getInt("userid");
String username = rs.getString("username");
String name1 = rs.getString("name");
System.out.println(name1);
li_row++;
} } catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Upvotes: 3
Views: 11611
Reputation: 46796
The way PHP and Java approach development is very different.
The approach which Java mostly uses is similar to PHP's Zend framework.
There is a vast number of frameworks which allow you to externalize the configuration.
Since it seems you just start with Java, I have to tell - it's a looong way to go... but I'd like to suggest to you, in long term, to look at Java Application Servers, e.g. JBoss AS. These servers call the JDBC connection "Datasources", so that's what you want to configure.
In those, one typically uses JPA - Java Persistence API, which uses a file persistence.xml
which looks like this:
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="educationPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/COE" />
<property name="hibernate.connection.username" value="root" />
<property name="show_sql" value="true" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
(in this case, with DB properties defined, for illustration, but the datasources usually define that in AS)
Then you create something called EntityMangerFactory
and get a "connection" (EntityManager
) from it and call the methods to manipulate data or execute SQL.
If your application is standalone, then you may have a look at so-called IoC frameworks like Weld-SE, Guice, Spring Framework, et al, which allow you to put the config and structure of your app to external XML (and other) files.
And if you don't want to use any of these, then create your own "DbManager" as it's usually called, but at least, for performance reasons, you're going to need a "connection pool", see e.g. C3P0.
Upvotes: 1
Reputation: 7871
One of the way to approach is to have a BaseDAO
with a method which returns the Connection
object, where you can have the following code -
public class BaseDAO {
public Connection getConnection() {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/saloon";
String name = "root";
String password = "";
Connection con = DriverManager.getConnection(url, name, password);
return con;
}
}
You can then extend this BaseDAO
in all the other DAO
classes or create an object of the BaseDAO
class and get the Connection
object.
Your remaining code would be in a separate DAO
class. (Lets call it GetDataDAO
).
public class GetDataDAO extends BaseDAO {
Connection con = getConnection();
Statement stmts = null;
String query = "select userid, username, name , address, hairstyle from saloonuser ";
stmts = con.createStatement();
ResultSet rs = stmts.executeQuery(query);
int li_row = 0;
while(rs.next()){
mysTable.setValueAt(rs.getString("username"),li_row,0);
mysTable.setValueAt(rs.getString("name"),li_row,1);
mysTable.setValueAt(rs.getString("address"),li_row,2);
mysTable.setValueAt(rs.getString("hairstyle"),li_row,3);
int userid = rs.getInt("userid");
String username = rs.getString("username");
String name1 = rs.getString("name");
System.out.println(name1);
li_row++;
}
}
Upvotes: 9