Database Connection In Java
Oct 27, 2014I'm using oracle 10g Database and Java 'jdk1.7.0_45' version.I have to a create program to insert data into a table.
View RepliesI'm using oracle 10g Database and Java 'jdk1.7.0_45' version.I have to a create program to insert data into a table.
View RepliesI have a shop and I use visual studio.net to create an Inventory application to manage my shop. So I am familiar with vb.net codes.
Now, I'm trying to built another Inventory application for my shop using java, 'coz now I'm using Linux Ubuntu for my OS desktop and there's no visual studio.net in linux.
So in vb.net, there's a module that I use for database connection that I can call from any form that I needed.
I searched google for the equivalent module for java and there is none. But, there's an example that use a new java class.
Here's the code in MySQLConnect.java :
Java Code:
public class MySQLConnect {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/dbInventory";
static final String USER = "root";
static final String PASS = "root";
public static Connection ConnectDb() {
[Code] ....
I am trying to use a custom listener to initialize database connection pool (C3P0) on start up and then destroy on context shut down. The reason for that is that I whenever context is shutdown I have a memory leak because initialized connection pool is not being destroyed.
I have a static class called C3P0Utils that deals with connection pool. In my listener in contextInitialized method I have tried at first to init the pool like this :
+public void contextInitialized(ServletContextEvent sce) {+
+try {+
C3P0Utils.newInstance().init();
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+
I know that object is created. I checked it using jconsole. However it is not accessible with in the application. My second attempt was to regester the pool and then add it to the context and then when I need it read from there.
+public void contextInitialized(ServletContextEvent sce) {+
+try {+
ServletContext ctx=sce.getServletContext();
C3P0Utils.newInstance().init();
ctx.setAttribute("myDataSource", C3P0Utils.newInstance().getDataSource());
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+
However when I try to red from the context I get nothing. I try to read like this.
+(ComboPooledDataSource)ctx.getAttribute("myDataSource")+
When I print names of all of the attributes in the context I get these attributes.
Context Name
org.apache.tomcat.InstanceManager
org.apache.catalina.jsp_classpath
javax.servlet.context.tempdir
org.apache.catalina.resources
+org.apache.tomcat.JarScanner
+org.apache.jasper.compiler.TldLocationsCache
+org.springframework.web.context.WebApplicationContext.ROOT
Why I can not use anything initialized in the listener.
I was wondering if I could take the following code and somehow put the connection to database in it's own class without the "public static void main(String[] args)" method. That way I could call it anytime i want to open a connection to the database.
Java Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample {
public static void main(String[] args) throws ClassNotFoundException
[Code] ....
I'm using JSF framework. I can let Netbeans to create the persistence unit and hardcodes connection parameters into the persistence.xml file or i can use resource tag in context.xml file or using another file like property file or class for holding connection parameters like username and password; what is the most secure way or expert way to use connection properties in my app or there is no difference?
View Replies View RelatedI am trying to make a simple login using netbeans, derby database included in netbeans and tomcat server. I made everything nice and separated: I have a model package with a class called DbConnector that has the following method:
public Connection connect() {
try {
return DriverManager.getConnection(url, user, pass);
} catch (SQLException ex) {
return null;
}
}
Then i have another class, an userDAODB that has a password check method:
public boolean checkPassword(String user, String password) {
try (Connection con = new DbConnector().connect(); Statement stmt = con.createStatement()) {
//checks the password in the database
In the main method of this class i tested everything, it works very nice, logs me in, other methods work too, no problem what so ever.But then i go to my servlet:
public class LoginTest extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("username");
String pass = request.getParameter("pass");
UserDAODB userDAO = new UserDAODB();
boolean authe = userDAO.checkPassword(username, pass);
[code]....
So when i start the webpage and i try to click on the login button I get a NullPointerException com.model. UserDAODB. check Password (User DAODB. java:14) - so line 2 here
I have been googling a bit, i placed the derby.jar and the derbyclient.jar in the lib directory of tomcat, i tried to modify the context.xml of my application, but then it wouldn't even start anymore.
i have a problem in connecting my jsp program in MSsql 2005 i want to create a connection in my jsp program to MSsql database but theres an error of my work here is my code
Code:
PHP Code:
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*;" %>
<%@ page import="java.sql.*;" %>
<%@ page import="java.util.Enumeration;" %>
<html>
<head><title>Sample MS connection</title></head>
[Code]...
I am trying to make connection with oracle database but when i run java file it's raise an error
"classnotfoundexception oracle.jdbc.driver.oracledriver"
DriverManager.getConnection(
"jdbc:oracle:thin:@erp:1521:ORCL", "apps",
"apps");
I have small Java code, which execute every day and checks for data in database using Cronj Schedular and everything works fine, but recently I have observed that, it is failing due to
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.110.xx.xxx:1521/test
At the same time, when I run my test code to check Database connectivity that works fine without above exception. I'm unable to figure it out. Although, there was just slight code change, but that was nowhere related to Database or Database connection.
dbconf.java
public class dbconf {
private Connection connect;
private String connstr;
public Connection getConnection() throws SQLException {
connstr = "jdbc:oracle:thin:@160.110.xx.xxx:1521/test";
[Code] ....
Application Log file
Wed Jul 01 09:25:17 IST 2015:------- Initializing -------------------
Wed Jul 01 09:25:17 IST 2015:------- Scheduling Jobs ----------------
Wed Jul 01 09:25:17 IST 2015:------- Job Started Running ----------------
Thu Jul 02 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test
Sat Jul 04 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
Sun Jul 05 06:00:00 IST 2015 : Job Executed..!! Bschedularv2.2
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@160.xxx.67.xxx:1521/test
So, you can see, It failed on 3rd of July and 5th July as well. But, in between it ran fine.
We have a J2EE business web application that runs in WebLogic. We noticed in application log files errors messages that seems like "No resource available in db pool", it means all the connection objects (to Oracle database) are used. Then the application is unstable and unseable.
I think the maximum number of connections configured in Weblogic pool has been reached due to an increase of users and activity. But when the workload decreases the application is still out of service. It seems connections objects are not relased to the pool. What can be the explanation for this issue ? Normally it is managed by the container (EJB3 for the business tier).
Once the limit of the pool is reached it is necessary to restart the server to solve this kind of error ?
I have created a JSP page, on click of a particular button, the control moves to the servlet "TestServlet".
This is the code under TestServlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
[Code]...
Now, I have got a comment saying I should get the database connection from connection pool. And one more issue is that I have used the function System.getProperty("user.name") to get the username which i have logged in. But this code will be run on Unix which will not support this function. Any function where I can get the windows username. There is a function getUserPrincipal(), but this function returns a NULL. How to resolve this.
I configured a connection pool in tomcat 7. For every database activity I get a connection from the pool. Do I have to close the connection like other regular connection after the database operation is done? If I close the connection will it have any effect on the connection pool?
View Replies View RelatedI have created a sample java web service client using Apache Axis. It works well with basicHTTPbinding (without security and Basic authentication).I am not able to make a secured communication. How to make it work in a secured way using NTLM or any other security.
View Replies View RelatedI am trying to connect to an oracle rac using jdbc thin . when i use the scan as the host, like this
String url = "jdbc:oracle:thin:@//<scan>:port/servicename;
I get error 1153, connection refused
but when i use the ip of the rac instead of the scan
String url = "jdbc:oracle:thin:@//<ip>:port/servicename;
The connection is successful
Is the issue at the application level? or is the problem with the server ....
I am working on netbeans IDE , but I understand how the application know the parametres for the connections. I understand what is a Context , specifically i understand this code about the init method
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
servicioConexiones = (DataSource) envCtx.lookup("jdbc/bd_tutorias");
I want to know how the application knows to use these connection parameters, on the Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/tutorias_pag480">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="8" maxIdle="4" name="jdbc/bd_tutorias" password="mysqladmin"
type="javax.sql.DataSource" removeAbandoned="true"
url="jdbc:mysql://localhost:3306/bd_tutorias" username="root"/>
</Context>
I'm working in project, my theme is to develop an application management system for fingerprint and RFID card attendance machine in java programming language.
My problem i didn't find documentation in java for connecting with this device.
I want lo list all the devices connected to my network, I done like this
InetAddress i = InetAddress.getLocalHost();
byte[] ip1 = i.getAddress();
InetAddress address = InetAddress.getByAddress(ip1);
for (int b = 0; b <255;b++) {
ip1[3] = (byte)b;
[Code] ....
It prints all the connected devices, its k.. my doubt is, how to list the wired connections and wireless connections from them...
I am using EJB (Stateless) without any web service. I simply need to send a String from Client to Server
I can connect any Java SE Client with this EJB. But how I can connect an Android Client with this EJB (i.e. Using EJBRemote Interface) directly? I am using :
Glassfish Application Server
Netbeans IDE
I cannot restore my database that was backup already. This is my code . . .
public boolean restoreDB(String dbUserName, String dbPassword, String source) {
String[] executeCmd = new String[]{"C:Program Files (x86)MySQLMySQL Server 5.1inmysql.exe",
"--user=" + dbUserName, "--password=" + dbPassword,"-e", "source "+source};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
[Code] .....
how can i stop this loop(while (res.next() )
example: i hava a table conex wich i inser on it two columns name and password i fill it by
name : yosra password : lol
name : najeh password :mdr
i don't know when i fill jTextfield of password and name correctly for ewp i put yosra as name and password as lol the loop continue to the next row and i show the two message dialog on netbeans about correct acces and refused access and the frame of my chatroom is opened
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String pass=new String(this.pass.getPassword());
String nam=this.name.getText();
acceder(nam,pass);
[code]....
I want to add this image to database how can i do.
View Replies View RelatedI want to make a database and use it in my java program. I am thinking of using MS Access database, although I m not very sure, sine I will have to use this data later in the SQL database as part of C# program.
How to start to create and use MS database in java program.
I have seen in one tutorial that the steps are:
1. Install your database management system (DBMS) if needed
2.Install a JDBC driver from the vendor of your database
but I am not familiar with this. Any example, or is this above compulsory?
i want o know that how can i connect my sql database to java
View Replies View RelatedIn MVC, M is the model, V is the view and C is the controller. But whatever projects I have made till now, I have a separate layer called data layer where I do all the database operations. In MVC, are we saying that the database operations are within the controller layer? That's quite a little mismatch! Separation of layers yet merge of controller & database operations!!
View Replies View RelatedI would like to make a database using HTML and Java. I already made something like this using swing. I am just looking for some pointers here. I just started looking into Java Play 2 and I have a feeling this is what I am looking for. JavaEE is very complicated and I have read that it is being phased out. What is your opinion on this?
I want to make a static HTML page and put it on my home network and treat one of my computers as the sever accessing mySQL.
I'm having problem with my code when it comes to Restore Database in Java. This is my code:
public boolean restoreDB(String dbUserName, String dbPassword, String source) {
String[] executeCmd = new String[]{"C:Program Files (x86)MySQLMySQL Server 5.1inmysqldump ", "--user=" + "root", "--password=" + "1234" + source};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
[Code] ....