Is there a PLATFORM INDEPENDENT connection string for EXCEL file in java. jdbc:odbc is platform dependent. Is there anything else ??
In this post you could see an example of using a connection string without the ODBC. I suppose this is what you are looking for...
Class.forName("com.hxtt.sql.excel.ExcelDriver").newInstance();
String url = "jdbc:Excel:///E:/JavaWithExcel/Feedback.xlsx";
String sql = "select * from [Sheet1]";
Connection con = DriverManager.getConnection(url, "", "");
Statement stmt = con.createStatement();
System.out.println(con);
System.out.println(stmt);
stmt.close();
con.close();
Anyway, you have to understand that the connection string is always specific to the underlying environment. It could depend on the operating system, on the file system or something else... I would just use different configuration files for each environment.
Hope I helped!
Related
Is it possible to find the sizes in GB of the hive tables using Java/jdbc? I don't want to depend on the hive warehouse folder in HDFS (as described in link) as different tables may have different locations
If you are mention 'totalSize' from 'tblproperties' then it is possible with similar approach:
String driverName = "org.apache.hive.jdbc.HiveDriver";
String connectionURL = "jdbc:hive2://HOSTNAME:PORT/default";
try {
Class.forName(driverName);
Connection connection = DriverManager.getConnection( connectionURL, "", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Statement stmt = connection.createStatement("show tblproperties TABLENAME");
ResultSet rs = stmt.executeQuery(stmt);
while(rs.next()){
//doWhatYouWant
}
Currently not be possible but it can be done .
To get file size you have to run on file system( HDFS) commands .
In case of RDMS data bases ie sql server have encapsulated file system commands in SYS views and SYS functions (DMF and DMVs) .
If some one write or develop such UDF it will be possible but Internally the UDF will be calling same comand.
This question already has answers here:
findbugs and database password security issue
(5 answers)
Closed 7 years ago.
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
PreparedStatement st = con.prepareStatement(
"INSERT INTO menu(menu.menuID,menu.name,menu.info,menu.price) values(?,?,?,?)");
st.setString(1, value1);
st.setString(2, value2);
st.setString(3, value3);
st.setString(4, value4);
st.executeUpdate();
JOptionPane.showMessageDialog(p1, "Data is successfully inserted into database.");
con.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(p1,
"Error in submitting data!");
}
I ran FindBugs and this is the bug that is coming on line 3:
Hardcoded constant database password in ie.lyit.flight.Changeadd$3.actionPerformed(ActionEvent)
This code creates a database connect using a hardcoded, constant password. Anyone with access to either the source code or the compiled code can easily learn the password.
Rank: Scary (7), confidence: Normal
Pattern: DMI_CONSTANT_DB_PASSWORD
Type: Dm, Category: SECURITY (Security)
I was wondering if anyone knows how to get rid of this bug and how I would go about doing it?
Code analysis tools check for any loop-hopes in code along-with looking for best-practices (or violations of them).
While developing, you can ignore such warning, but yes, once you done with your business logic, its always good to apply best practices - in this case read the password from a configuration or properties file.
If you are using jtd for connecting database,there is no need to provide username and password for connection.Try below code-
Connection conn = null;
String url = "jdbc:jtds:sqlserver://" +serverName+ "/" +"master";
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver);
conn = DriverManager.getConnection(url);
master is database name in my case.Just replace it with yours.
This simply tells you that passwords should not be stored directly in the source code of your application, because it is often shared and not encrypted.
Use some external source instead, and even better do not store any passwords, store password hashes only.
You may also look at:
https://stackoverflow.com/questions/tagged/pbkdf2
How can I hash a password in Java?
https://stackoverflow.com/questions/tagged/ldap
http://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html
I have some non-standard characters in my Access 2010 database. When I read them via
Connection con = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.util.Properties prop = new java.util.Properties();
prop.put("charSet", "UTF8");
String database = "jdbc:odbc:Lb";
con = DriverManager.getConnection(database, prop);
} catch (Exception ex) {
System.out.println("Error");
}
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery("SELECT distinct forename, surname from PSN where isValid");
while (rs.next()) {
String forename = rs.getString("forename");
}
I receive question marks (?) where the character should be. Why is this?
I had question marks when DB contained polish characters. It was fixed when I set charecter encoding to windows-1250.
def establish(dbFile: File): Connection = {
val fileName = dbFile.getAbsolutePath
val database = s"${driver}DBQ=${fileName.trim};DriverID=22;READONLY=true}"
val props = new Properties()
props.put("charSet", "Cp1250")
val connection= DriverManager.getConnection(database,props)
connection
}
I expect your JDBC driver to handle reading and writing characters to your database transparently. Java's internal string representation is UTF-16.
Java(UTF-16) --JDBC--> Database(DbEncoding)
Database(DbEncoding) --JDBC--> Java(UTF-16)
Perhaps the problem is that you are trying to force reading them with UTF8 and the database uses another internal representation?
Also, how do you verify that you receive '?'
If System.out is involved, you should take into consideration that this PrintStream converts in memory Strings to the Charset that it uses. IIRC this Charset can be found with Charset.defaultcharset() and is a property of th JVM that runs the program.
It is preferable to inspect the hexadecimal value of the char and look up a Unicode table to be sure that information has been lost while reading from the database.
Hope this helps a bit.
It's not "utf8", "Cp1250" !
One must use : ISO-8859-1
java.util.Properties prop = new java.util.Properties();
prop.put("charSet", "ISO-8859-1");
String connURL = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + accessFileName + ";uid=''; pwd='';";
sql = "SELECT * FROM enq_horaires;";'
con = DriverManager.getConnection(connURL, prop);
stmt = con.createStatement();
ResultSet rset = stmt.executeQuery(sql);
This is a long-standing interoperability issue between the Access ODBC driver and the JDBC-ODBC Bridge. Access stores Unicode characters using a variation of UTF-16LE encoding (not UTF-8) and the JDBC-ODBC bridge is unable to retrieve them.
(Note that this is not a problem with the Access ODBC driver per se because other tools like pyodbc for Python can retrieve the Unicode characters correctly. It is a compatibility issue between the JDBC-ODBC Bridge and the Access ODBC driver.)
A bug report was filed with Sun in November 2005 outlining the issue. That report was closed as "Won't Fix" in April 2013 with the comment
The bridge has been removed from Java SE 8 and is not supported
If you need to work with arbitrary Unicode characters in an Access database you should consider using UCanAccess. For more information, see
Manipulating an Access database from Java without ODBC
I would like to make o program that would store data from excel files in databases. I have plenty of databases so in my program i have to choose in which one I will store the data.
I have made the code to be able to connect mysql with my program and to show the available databases. What I would like to do now is to say in which database i would store the data.
To be more specific I would like the user first of all to see tha available databases in his client and afterwards he would have the chance to say in which database the data would be stored.
Could anyone help me how I would do this?
The code to see all the available databases is the below:
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()){
System.out.println (" " +res.getString(1));
}
Thank you in advance!
I hope this SO link should help you . You can get all the data from the connection object
First do a create a simple connection
Connection con = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost:3306/", "root", "root");
Look for an example here
see how he uses ResultSet Class to access the result.
Now,
retrieve information from information_schema.SCHEMATA so you can query like
SELECT schema_name FROM information_schema.SCHEMATA S;
to get all the schemas
Next after getting choice from user(maybe from console) you can set database according to uservalue using Connection#setCatalog()
If you want table information use this query
SELECT * FROM information_schema.TABLES T;
It would list all the tables in all schemas
I have a problem with a really slow connection between my Java code and a MySQL Database. I don't know where the bottle neck is.
My program is more or less a chatbot. The user types something in, my program splits the sentence into words and sends it word per word to the database. If it finds something there, the user gets an output.
The database is on an external Server, but I also tried to connect to a pc next to me. Both is slow.
I tried the connection once at another place then where I normally work and there it was fast, most of the time.
My SQL Code:
SELECT info.INFORMATION FROM INFORMATION info, INFO_SCHLUESSEL sch
WHERE LCASE(sch.SCHLUESSELWORT) LIKE '" + input + "%' AND info.ID_INFO = sch.ID_INFO
Order BY info.PRIORITAET DESC LIMIT 1;
(just remembered, if it helps to understand the sql code:
schluessel = key
Schluesselwort = key word
prioritaet = priority)
My Java Database Code is more or less standard stuff:
String driver = "com.mysql.jdbc.Driver";
String dbase = "jdbc:mysql://bla";
String dbuser = "bla";
String dbpw = "bla";
Class.forName(driver);
Connection con = DriverManager.getConnection(dbase, dbuser, dbpw);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
ergebnis = rs.getString("info.INFORMATION");
}
rs.close();
stmt.close();
con.close();
edit:
I have tried this DBCP for a while now, and I can't seem to get it to work. It seems to be as slow as the old connection. This is the example provided by the website that I use:
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://bla", "bla", "bla");
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDriver driver = new PoolingDriver();
driver.registerPool("example",connectionPool);
Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
I suspect that it's the connection setup that is causing the problem. It would be worth timing how long this takes:
Connection con = DriverManager.getConnection(dbase, dbuser, dbpw);
and if so, check out Apache Commons DBCP, which allows you to pool database connections.
Well I think this warrants a discussion on the design.There are a few things which you can do in order to improve the performance. Since you are not persisting anything here, its better to preload all the data in memory in some custom java object, a map, list or whatever and then do an in-memory lookup for the word and get the results. Another approach could be to use a batch statement so that you dont go ahead and create and release connections for each word. Oh and if using batch statements make sure you set the batch size to an appropriate number, preferably a prime number