How can I connect Access Database with Netbeans on Mac OSX? - java

I got a access database, and I'm programming in Java.
What can i use to connect my netbeans with my database on localhost?
Only found this code(it uses local db file) for Windows:
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
"(*.mdb, *.accdb)};DBQ=C:\\Database\\Northwind 2007.accdb";
Connection con = DriverManager.getConnection(url);
System.out.println("Connected!");
con.close();
} catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+cE.toString());
}
Maybe someone know how to modify it for mac environment?

Take a look at using the UCanAccess JDBC driver available to download. Include all the jar files in the libraries of the project and you should be able to make a connection to your access database without having Access installed. UCanAccess works best with NetBeans.

In Access Database Manipulation via JDBC is explained step by step.

Related

MySQL connection JDBC driver not found

I am trying to open a MySQL connection but run into this error.
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
There are plenty of questions online about this but I cannot seem to make it work.
I currently using gradle to compile 'com.oracle:ojdbc14:10.2.0.4.0' and I have added the connector library using the build in maven importer.
My code looks like this:
private static final String DRIVER = "com.mysql.jdbc.driver";
...
try {
try {
Class.forName(DRIVER).newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
...
}
I tried to add ojdbc-14.jar to libs and reference that in gradle without success. I also use some groovy code in the build.settings but without success, I'm not sure how to work with that either.
A pic of my IDE:
The error is in your DRIVER string. It should be
DRIVER = "com.mysql.jdbc.Driver"

No suitable driver found for jdbc java Tomcat 8

Trying to connect my java web application to mysql database using tomcat 8 server and getting same error every time: No suitable driver found for jdbc
this is my method that is trying to connect to database
public String kreiraj() {
String g="com.mysql.jdbc.Driver";
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting database...");
String url = "jdbc:mysql://localhost:3306/fc";
String user = "root";
String password = "";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
return "index"; }
If I start same code as standard java application it work and it connects to database without any problems.
So I figured that the problem must be something with tomcat 8 server. I tried every possible solution there is and im still getting same error.
I tried this things:
adding mysql connector jar file to build path
adding mysql connector to WEB-INF/lib folder of my web application
adding mysql connector to tomcat 8 lib folder
tried using Class.forName("com.mysql.jdbc.Driver"); but getting classnotfound error
Nothing seems to work and im starting to lose my mind. If anyone know solution it would be great. Ty

Connection to a derby database in a dynamic web project

I am using the following code to connect to a derby database in a dyamic web project but it does not work. If I use the same code in a normal java project it works can you guys help me out.
private Connection connection = null;
public DBconnection(){
createConnection();
}
public void createConnection(){
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
this.connection = DriverManager.getConnection("jdbc:derby://localhost:1527/student");
}catch (Exception e){
e.printStackTrace();
}
}
public Connection getConnection(){
return this.connection;
}
Note:
I have included all the derby libraries in my dynamic web project and also I have I have started the derby server everything works but this code does not connect to the database. Also I am running the dynamic project on a tomcat server the port for that is 8080 and the port for the derby server is 1527.
Please copy the derbybyclient.jar file which is present in derby installation lib folder into the lib folder of your tomcat installation.
Rest of the code is fine.
For more information you can refer this link

Can't add JDBC driver to Library scope

I am currently trying to contact a MySQL server.
My goal is to have a servlet that will show some table data when run.
My servelt is running on glassfish 3.1.
Here is the simple doGet method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("<h1>select * from userName:<h1>");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/people", "root", "r00tpw");
System.out.println("Connected");
PreparedStatement statement = (PreparedStatement) conn.prepareStatement("select * from userName");
ResultSet result = statement.executeQuery();
while(result.next()) {
out.print(result.getString(0));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
I have got the correct JDBC driver. The problem I have is with my source code finding it. I have tried right clicking JRE System Library->Build Path->Configure Build Path and adding an external JAR. Rather than adding the JAR to Referenced Libraries it seems to be added outside of any library scope. This means that Class.forName("com.mysql.jdbc.Driver"); is always throwing a ClassNotFoundException.
I have also tried adding the JAR to my Run Configurations->Glassfish 3.1 at [pcname]->Classpath->User Entries with no luck.
I have also tried using MS SQL Server and their JDBC driver. The same thing happens.
I have attached an image of my project structure to help.
Put the mysql-connector-java-5.1.26 to your WebContent--->WEB-INF-->lib.inside lib folder.
First of all after you install jdbc driver you need to configure it in glassfish. Create a JDBC resource and JDBC connection pool.
PS Did you add the jdbc JAR to glassfish server folder?
For glassfish too see your connector it must reside in \Glassfish3\domains\domain1\lib\databases
May be you are not added mysql driver to a server folder. Therefore, did you check your glassfish-resources.xml or JDBC connection in Glassfish admin console?

how to connect mysql database to desktop java application using jdbc?

how to connect mysql database to desktop java application using jdbc ?
the environment is ubuntu 12.04 Lts i tried so many ways but all of them throw an exception on executing
Class.forName("com.mysql.jdbc.Driver");
the exception say :
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
the full code is :
import java.sql.*;
public class CRUDForGoods {
private Connection connection ;
public Vector<GoodsStore> goods ;
public CRUDForGoods(){
try{
DriverManager.registerDriver(new JdbcOdbcDriver());
connection = DriverManager.getConnection("jdbc:odbc:dbName","root", "root");
}
catch(Exception ex) {
System.out.println("##connection");
ex.printStackTrace();
}
}
com.mysql.jdbc.Driver not found while executing the code.
You need mysql connector to get it run (the jar includes com.mysql.jdbc.Driver). You can find it here
Download and include the jar file in your classpath.

Categories