Connect Xampp to Netbeans IDE (8.0.2) - java

You can check the in the image(link at the end of sentence) the library and code as well ---
The error is java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. I already have mysql-connector-java-5.1.23-bin and it's already pasted in my Webserver's lib directory. Below is the code.
package databaseinsertexample;
import java.sql.*;
import java.sql.DriverManager;
public class DatabaseInsertExample {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myteam","root","");
String sql="insert into teams values(?,?,?)";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setString(1,"India");
stmt.setString(2, "Niraj");
stmt.setInt(3, 100);
stmt.execute();
con.close();
} catch(Exception e)
{
System.out.println(e);
}
}
}
On the Localhost site of XAMPP, the Database has been created properly but when I'm executing the program on my Java file via Netbeans it's not reflecting on the Database. Please help I'm new to this Programming world, I checked on YouTube still I'm not able to clear this doubt of mine.

change your clase forName like this.
Class.forName("com.mysql.jdbc.Driver");
And also please refer this question - How to connect XAMPP MySQL local DB using JDBC?

Related

JSP error java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver

thing used : eclipse, java 1.8.0 version, mariadb 10.2, mariadb-java-client-2.4.1jar
problematic point : I have only used oracle db until now, but it is my first time using maria db.
Integrated with db in eclipse>Util.java. However, when you create a jsp page and enter data(Data query results ("select * from..." sql statement with db)),
the [java.lang.ClassNotFoundException:org.mariadb.jdbc.Driver] error appears.
The jdk version I'm using is 1.8, so I have to use 52.0,
but I don't know what to Whether to use. I've already used 2.x among jar versions. But no. I'd appreciate it if you could help me solve it.
my Util.java code
package DBPKG;
import java.sql.*;
public class Util {
public static Connection getConnection() throws Exception {
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/board",
"root",
"1234");
return conn;
}
public static void main(String[] args) throws Exception {
System.out.println(Util.getConnection());
}
}
Console Results
org.mariadb.jdbc.MariaDbConnection#58fdd99

can I connect MySQL database file .ibd located on desktop or any directory to java?

I would like to link java to different directory database folder like on desktop or any path,
conn=DriverManager.getConnection("jdbc:mysql:C:\\Users\pc\Desktop\sample","root","root");
this code here work fine
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","root");
here is full code that I work on :
import java.sql.*;
import javax.swing.JOptionPane;
public class mysqlconnect {
Connection conn=null;
public static Connection ConncerDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/sample", "root","root");
} catch (Exception e){
javax.swing.JOptionPane.showMessageDialog(null,e);
}
}
}
by the end i would like to build project on java with MySQL database and I can run that project as .jar file and database can be still connected

Embedded Database into Jar File

For the past couple of days I have been trying to get my Derby database to be accessed in my jar file. Here is what my connection class looks like:
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class DBConnection
{
public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby:EmployeeInfo";
public static Connection dbConnector()
{
try
{
Class.forName(DRIVER).newInstance();
Connection conn = DriverManager.getConnection(JDBC_URL);
JOptionPane.showMessageDialog(null, "Connection successfull");
return conn;
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Also, here is a screenshot of what my project explorer looks like:
Everything works when I am in eclipse, my program runs as expected with my GUI updating the Database. But the minute I make my program a jar file it says it can't find my database EmployeeInfo (note: Database.jar is the EmployeeInfo database). One last thing, when I try the jar file out on a different machine it also states that it cannot find the database.
An explination on why this is going along with any fixes would be great!
-Thanks,
Aaron :)
Well i eventually found the fix :). I had to place the embedded derby database files into a folder along with the derby jars, and my jar file that contained my project and everything worked beautifully!

Connecting to a mySQL database using Java

So I just started learning about databases this week and one of the things I need to be able to do is connect to my mySQL database that I created using Java. I have done some research and I have tried to find the correct way of doing this I just can't seem to figure out how. Here is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Menu
{
public void menu()
{
Connection conn;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "gym";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
System.out.println(e.getMessage());
}
}
}
Now the problem is, is that every time I run this code, the "No Connection =( " appears and then it says the error is: "com.mysql.jdbc.Driver". Can somebody please help me and say what I am doing wrong? Thank you. Much appreciated.
Your error means that your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class.
You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path (where x.x.x is the version of the jar).
All JDBC connection classes need their respective drivers which are normally supplied as jar files from the database vendor, add the relevant database driver to your classpath.
The .jar file will be available from the vendors site, in this case : http://www.mysql.com/products/connector/ and then to add this to the classpath of your project in the ide of your choice. Here is a guide for eclipse : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)
Try and run again once you have this.

Java connection to MS Access Database

Just wondering if anyone cane help me, I'm trying to connect to an MS Access Database. I have done it on other projects and used exactly the same code. Can anyone see if I have done anything wrong?
try {
System.out.println("Attempting Database Connection");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver(*.mdb)};DBQ=MotivationDatabase.mdb;";
connection = DriverManager.getConnection(sourceURL, "", "");
stmt = connection.createStatement();
System.out.println("Connection made");
} catch (Exception e) {
System.out.println("Database connection attempt failed");
System.out.println(e);
}
I keep getting the error:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'.
But my database is in the same folder as my project like I've done before s I'm not sure why i am getting this error. Help?
Control Panel -> Administrative Tools -> ODBC Data Sources -> Add -> Microsoft Access Driver(*mdb,*accdb)
Specify the correct path to MotivationDatabase.mdb corresponding to Data Source name and save the settings.
Refer here.
Code:
public class Main {
#SuppressWarnings("unused")
public static void main(String[] args) {
try {
System.out.println("Attempting Database Connection");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="
+ "D:\\MotivationDatabase.mdb";
Connection connection = DriverManager.getConnection(sourceURL);
System.out.println("Connection made");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
P.S: Please learn to work with JDBC as JDBC-ODBC Bridge will be removed in JDK8.See here.
EDIT:
You can also use JDBC along with UCanAccess API to connect to an MSAccess database. You would need the following jars in your project build path.
commons-lang-2.6.jar
commons-logging-1.1.1.jar
hsqldb.jar
jackcess-2.1.0.jar
ucanaccess-2.0.9.5.jar
Code:
connection = DriverManager
.getConnection("jdbc:ucanaccess:////REMOTE-IP-ADDRESS/shared-folder/TestDB.mdb");
System.out.println("CONNECTION ESTABLISHED....");
Works fine with JDK8. You can download the entire source code from here.
Sun JDBC ODBC will notte work with MS access when java 8 will ne release: I suggest you to use apache poi project. It s simple and works great.
Yeah it's correct the right project is jakcess:
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
try{Table table = DatabaseBuilder.open(new File("filename")).getTable("tablename");
righe.add(0);
for(Row row : table) {
String articolo=row.get("ColName").toString();
Try this class forename and connection URL.Add the below jar files to your projects:
commons-lang.jar,commons-logging.jar,hsqldb.jar,jackcess.jar,ucanaccess.jar
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//change the path with your own accdb file
String URL = "jdbc:ucanaccess://D:\\projects\\test.accdb";
Connection con = DriverManager.getConnection(URL);

Categories