I am browsing this website for quite some time now. The community helped me quite a lot, even though I never registered. Thanks for that.
However, this time I can't find a solution to my problem just by browsing, so I decided to register here and state my question so that someone might be able to help me.
First of all I'd like to say, that I'm basically at the beginning of my studies, so I'm not very knowledgeable yet. At the moment I'm learning for a upcoming exam and I need to know about databases. The Database of choice is Postgresq. But to really understand something like that, you need to try it out and not just read about it.
I'm not exactly sure what information are exactly required. But I'm using Windows 7, pgadmin and eclipse for that, all of them should be up to date. If you need further information, please ask for it.
I used a Tutorial to get it up and running, however I'm not able to establish a connection to the database using eclipse. The DriverManager.getConnection() causes a UTF-8 error I'm not understanding nor able to fix it.
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:67)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:212)
at org.postgresql.Driver.makeConnection(Driver.java:407)
at org.postgresql.Driver.connect(Driver.java:275)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.hywy.TestConnection.main(TestConnection.java:23)
Caused by: java.io.IOException: Illegal UTF-8 sequence: initial byte is 11111xxx: 252
at org.postgresql.core.UTF8Encoding.decode(UTF8Encoding.java:131)
at org.postgresql.core.PGStream.ReceiveString(PGStream.java:331)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:447)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:223)
... 7 more
The Code I'm using is:
import java.sql.*;
import java.util.Properties;
//import com.hywy.db.DbContract;
public class TestConnection {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("user", "postgre");
properties.setProperty("password", "random_password");
//properties.setProperty("ssl", "true");
String url = "jdbc:postgresql://localhost:1112/people";
try {
Class.forName("org.postgresql.Driver");
// Connection c = DriverManager.getConnection(
// DbContract.HOST+DbContract.DB_NAME,
// DbContract.USERNAME,
// DbContract.PASSWORD);
Connection con = DriverManager.getConnection(url, properties);
//Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:1112/people" , "postgre" , "random_password");
System.out.println("DB connected");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
The code is a bit of a mess, since I Googled some time for it and tried some stuff. I was not sure if I should paste the out-commented code with the rest of it, but at least you can see that I already tried some things.
I also tried a different port, at first I was using the standard port (5432) but it made no difference.
I also linked the up-to-date jdbc jar file in my build path.
I hope someone knows (about) this problem and is willing to help me.
Thanks in advance.
Excuse my English, it's not my native tongue.
Ok guys, I just wanted to say that I fixed the problem. I also wanted to thanks anyone who tried to help me.
It's only logical that no one was really able to help me, because it was only due to my stupidity.
The mistake happened at
"jdbc:postgresql://localhost:1112/people"
Only when I did the whole thing on another computer, to see if it's my system, I realized I'm not supposed to write the actual table name at the end of the line, but instead the name of the Database, so I just changed "people" into "postgres" and it worked.
Sorry for the stupid question, I hope my next one will be better :)
*Topic can be deleted or closed or whatever you want to do with it.
Thanks anyone!
Related
I'm trying to make a program that connects to an Oracle database for the ultimate purpose of creating a few tables and running commands on them for a course I am taking. I'm currently trying to make the example given in class work but I can't get that to work. The code that generates the error "java.lang.ClassNotFoundException" is when my main hits the code:
Class.forName("oracle.jdbc.OracleDriver");
or
Class.forName("oracle.jdbc.driver.OracleDriver");
I have manually added the ojdbc6.jar, ojdbc8.jar and ojdbc14.jar files to each folder in my "PATH" system variable and I'm getting the exception:
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
or
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I have no idea how to fix this or what to modify. It is a course example after all...
Any/All suggestions are welcome.
Thank you for your time
I hope this might help?
I don't think you need Class.forName with latest version.
I would have done something like below and set connection,url,user and password as global final variables to avoid repeating yourself and call the method where you need it.
public Connection getConnection() throws SQLException {
return connection = DriverManager.getConnection(url, user, password);
}
I know it's an old question but the case is different here. It seems to be a bug at JAVA 10 or MySQLConnector/J 8.0.12 jar classes.
The same projects (lots of them, not just one) that worked fine with JAVA 8 and MySQLConnector/J 5.1.39, which I used before, now don't work after I updated my system.
But the error appears only with RowSet. With DriverManager/Connection/ResultSet, explicitly used, it works pretty fine with the same url, url-parameters and MySQL driver.
I've tried to redundantly use Class.forName("")... as well as variations of the url parameters, but nothing works when I use RowSet. The same java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/scheme1?verifyServerCertificate=false&useSSL=true is thrown again and again.
Why the same code works with Java 8 and not Java 10 ?
try {
RowSet rowSet = RowSetProvider.newFactory().createCachedRowSet();
String url = "jdbc:mysql://localhost:3306/scheme1?verifyServerCertificate=false&useSSL=true";
rowSet.setUrl(url);
rowSet.setUsername("root");
rowSet.setPassword("root");
rowSet.setCommand("select * from client");
rowSet.execute();
while(rowSet.next()) {
System.out.println(rowSet.getInt(1)+" "+rowSet.getString(2));
}
} catch (SQLException e) {
e.printStackTrace();
}enter image description here
(See pictures with the error).
If someone know the problem and a solution please help. I'd like to keep using Java 10.
Daniel Pinheiro
danielpm1982#gmail.com
mysql-connector.jar is ment for JAVA 8
You need to wait till MySQL rollout the connector for JAVA 10 or downgrade to JAVA 8.
So I'm using the WinRegistry class, found in an answer on this page.
I need to stop my JAR file showing up in LastActivityViewer - which apparently uses Registry Keys to obtain it's information. Now on this site, it explains which registry keys to delete.
I specifically need to clear; Windows Prefetch Folder, Open/Save MRU list in the registry and the Windows Shell Bag Registry (you will understand if you look at the site I linked).
So this much I've figured out, but basically, the WinRegistry class has a deleteKey and deleteValue method which I can use - but how do I know the parameters that I should enter / which method to use?
Please help me on this; I've been looking everywhere and cannot seem to find an answer.
Proof of research:
Finding a specific registry key using java in windows
https://coderanch.com/t/517229/java/Editing-reading-windows-registry-java
Disclaimer: I've tried to word this question as understandable as possible. If it isn't clear then please leave a comment about what is unclear.
try {
//ls = WinRegistry.readStringSubKeys(WinRegistry.HKEY_CURRENT_USER,
// "Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\Shell\\");
WinRegistry.deleteKey(WinRegistry.HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSavePidlMRU\\jar");
} catch (Exception ex) {
ex.printStackTrace();
}
This is returning a NPE -> will try to fix, but in the meanwhile any help appreciated.
EDIT: I've been looking through regedit and I found this; OpenSavePidlMRU folder which contains "exe, jar, jpg, png" etc inside. I used the RegistryWin.deleteKey method and the jar file disappeared from the registry - this is a good thing but is it dangerous?
I just checked out the Spring Greenhouse project as a first step to learn Spring Security.
The project works fine but I was wondering about the following scenarios:
There are two configurations: standard and embedded. The javadoc says the embedded is default. I am not sure how to make it run in standard mode. Has anybody has tried this before?
Secondly in embedded mode I modified the code slightly with the following code to run it with MySql but to my surprise the application is not starting up at all. It throws the following error:
throw new RuntimeException("Unable to determine database version", e);
#Bean(destroyMethod="shutdown")
public DataSource dataSource() {
// EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
// factory.setDatabaseName("greenhouse");
// factory.setDatabaseType(EmbeddedDatabaseType.);
DriverManagerDataSource mysqldataSource = new DriverManagerDataSource();
mysqldataSource.setDriverClassName("com.mysql.jdbc.Driver");
mysqldataSource.setUrl("jdbc:mysql://localhost/greenhouse?useConfigs=maxPerformance&characterEncoding=utf8");
mysqldataSource.setUsername("root");
mysqldataSource.setPassword("mysql");
return populateDatabase(mysqldataSource);
}
Can anybody please help me on this?
I figured it out by myself.
There was a tag in web.xml and i changed there from embedded to standard.
The problem was in the sql query which was creating the database version table was not mysl complaint. There were other queries also which were not mysql complaint. I changed the sql query and now eveything worked like as expected. The next thing I am gonna do is use some kind og generic database query generator so that i don't have to change the query if in future I change my mind to use post gre sql in place of my sql.
Thank you for all your help and support.
Judging by the exception you're getting, the file you're interested in is GenericDatabaseUpgrader.java
More than half the code there refers to DatabaseVersion which is a table that is created if absent.
Since I doubt you already have it and judging by the exception type (SQLException), I'm inclined to say it's failing to create/insert/retrieve the referenced table.
You can check if you have the table and go from there.
Also, judging by the code you're trying to inject, I'd also look at connection = dataSource.getConnection() as a possible culprit.
P.S. Regarding standard/embedded, it appears you can VM parameters or maven settings to switch between the two. Please check the official forums and specifically this topic for info
I'm a junior java programmer and I've finally made my first program, all by myself, apart from school :).
The basics are: you can store data on it and retrieve it anytime. The main thing is, I want to be able to run this program on another computer (as a runable .jar file).
Therefore I had to install JRE and microsoft access 2010 drivers (they both are 32 bit), and the program works perfect, but there is 1 small problem.
It takes ages (literaly, 17 seconds) to store or delete something from the database.
What is the cause of this? Can I change it?
Edit:
Here's the code to insert an object of the class Woord into the database.
public static void ToevoegenWoord(Woord woord) {
try (Connection conn = DriverManager.getConnection("jdbc:odbc:DatabaseSenne")) {
PreparedStatement addWoord =
conn.prepareStatement("INSERT INTO Woorden VALUES (?)");
addWoord.setString(1, woord.getWoord());
addWoord.executeUpdate();
} catch (SQLException ex) {
for (Throwable t : ex) {
System.out.println("Het woord kond niet worden toegevoegd aan de databank.");
t.printStackTrace();
}
}
}
Most likely creating Connection every time is slow operation in your case (especially using JDBC-ODBC bridge). To confirm this try to put print statements with timestamp before and after the line that get Connection from DriverManager. If that's the case consider not to open connection on every request but open it once and reuse, better yet use some sort of Connection Pooling, there are plenty of options available.
If that's mot the case then actual insert could be slow as well. Again simple profiling with print statements should help you to discover where your code is spending most of the time.
First of all, congrats on your first independent foray. To answer your question / elaborate on maximdim's answer, the concern is that calling:
try (Connection conn = DriverManager.getConnection("jdbc:odbc:DatabaseSenne")) {
every time you're using this function may be a major bottleneck (or perhaps another section of your code is.) Most importantly, you will want to understand the concept of using logging or even standard print statements to help diagnose where you are seeing an issue. Wrapping individual lines of code like so:
System.out.println("Before Connection retrieval: " + new Date().getTime());
try (Connection conn = DriverManager.getConnection("jdbc:odbc:DatabaseSenne")) {
System.out.println("AFTER Connection retrieval: " + new Date().getTime());
...to see how many milliseconds pass for each call can help you determine exactly where your bottleneck lies.
Advise: use another database, like Derby, hsqldb. They are not so different from MSAccess, (= can use a file based DB), but perform better (than JDBC/ODBC). And can even be embedded in the application (without extra installation of the DB).