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
Related
I am trying to read a data from one database table using java + jdbc and trying to insert into another database table on different server in same session.
I have created 2 connection object(con,conn1), each pointing to correct database.
With 1st con object i am able to read the data but When it is going to write the data to another table using conn1 it is failing with error
ORA-00942- table or view doesnt exist.
I have also cross-checked that table is present and we can write to that table.
with a standalone class and static data i was able to write to that table.
Please let me know what is wrong in my approach.
Code sample:
public static void main
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection( "URL","uname","passwd");
//code to read from db and add to arraylist.
close resultset, con.
This connection obj points to new a database and url
Connection con1 = DriverManager.getConnection( "url","uname","passwd");
PreparedStatement pstmt = con1.prepareStatement("insert into SCHEMA_NAME.TABLE_NAME(columns) values(?,?,?,?,?,?,?,?)");
//code to iterate the arraylist populated above
pstmt.setint etc
pstmt.executeUpdate();
It fails after executeUpdate statement.
Yes, problem was with pstmt.setClob field. I changed it to pstmt.setString and took .toString() of clob object and it worked.
I m still puzzled why it throws table or view doesnt exist error.
I am using MS Access & MySQL ,in access input this word
کوردستان ی عیراق (it's kurdish language using unicode )
my code is :
try{
String path ="src\\Database.accdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection c = DriverManager.getConnection(""
+ "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path);
Statement s = c.createStatement();
ResultSet rs = s.executeQuery("select * from mytable");
rs.next();
jTextArea1.setText(rs.getString(1));
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage());
}
with access the output is ??????????????
but with MYSQL and the output is کوردستان ی عیراق
Why ??
thanks
If you are just trying to get data from an MS Access db and you don't need to run complex queries, you might want to check out the Jackcess project, which is a native, cross-platform Java API for opening MS Access files. it doesn't currently have support for running SQL queries, but it does give you access to all the data without going through the (flaky) jdbc-odbc bridge. it also has support for looking up data using indexes (via an IndexCursor).
(disclaimer, i am the primary author).
You should set a appropriate charset for the properties when you try to establish the connection, e.g.:
java.util.Properties prop = new java.util.Properties();
prop.put("charSet", "UTF8"); // Not tested..
Connection c = DriverManager.getConnection(
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ="+path, prop);
Look into the documentation for the JDBC-ODBC Bridge for further details
From microsoft forums:
If your connection character set is utf8, then you should run this query right
after connecting to DB:
SET NAMES 'utf8';
Also your DB and tables and columns should have utf8_general_ci or other type of
utf8 collation.
Hope this helps
I know that using JDBC we can create output for queries inside a database, but how can I get the output of the command SHOW PROCESSLIST as an output of a Java program.
In mysql we get it by: SHOW PROCESSLIST
But I was wondering if we can generate the output using Java?
You can use executeQuery for getting SHOW PROCESSLIST ResultSet.
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", password);
Statement stmt = null;
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SHOW PROCESSLIST");
while (rs.next())
{
System.out.println(rs.getString("Host"));
System.out.println(rs.getString("Id"));
System.out.println(rs.getString("User"));
System.out.println(rs.getString("db"));
System.out.println(rs.getString("Command"));
System.out.println(rs.getString("state"));
System.out.println(rs.getString("info"));
}
Depending on your MySQL version, you can perform a select on
SELECT * FROM information_schema.PROCESSLIST;
and you can do a where between the user, database, and host IP.
For example:
SELECT * FROM information_schema.PROCESSLIST WHERE db ="mycase" AND HOST LIKE "192.168.11.174%"
for a jdbc program, im required to make a connection an excel database. the connection is successfully made but wen entering values into it, its giving an "Operation must use an updateable query" exception.
here s the code:
String url="jdbc:odbc:Sample"; //CHANGE THE DATABASE NAME
Connection conn= DriverManager.getConnection(url,"","");
PreparedStatement prepstat = null;
String insert="INSERT INTO [Sheet1$] ([AccountID], [ProjectID], [PositionID]) VALUES (?,?,?)";
prepstat= conn.prepareStatement(insert);
prepstat.setString(1, accountID);
prepstat.setString(2, projectID);
prepstat.setString(3, positionID);
prepstat.executeUpdate(); // this is where the exception occurs
Did you specifically state that the connection was readwrite in your connection string?
I am not familiar with JDBC, but ODBC would be:
"Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ=C:\MyFolder\MyWorkbook.xls; ReadOnly=False;"
Excel is read-only by default: http://support.microsoft.com/kb/257819
It was just needed to uncheck the read only while creating the DSN.
I've been able to use Java and HTMLUnit to scrape a web page, however I'm unsure how I can get this to write the resulting data to a MySQL database on a remote hosted server. I also need this to happen at regular intervals (perhaps once a day) if possible, without having to manually run the program.
Is this possible and can I have any guidance, thorough or not, on how to do this?
Thanks!
Get your page's HTML into some String variable
String scrapedHtml = "";
Create a table in mysql with a text field
create table html_store (
id int primary key autoincrement,
content text not null
);
Use JDBC code to connect to the database and inset the data. Set the connectionURL and other parameters correctly
Connection conn = DriverManager.getConnection(connectionURL, user, password);
PreparedStatement pstmt =
conn.prepareStatement("insert into html_store (content) values (?)");
pstmt.setString(1, scrapedHtml);
pstmt.executeUpdate();
pstmt.close();
conn.close();