How to Create a MySql Database using JDBC - java

Can we create a new MySQL database( not a table, I want a new database) from java code ? I have searched the internet and I get code only for connecting with existing mysql databases. Can i create a new database at run time using java code.
please help

In your connection string omit a database name and then execute Create database command.
Connection String: jdbc:mysql://localhost
Create Database Syntax : stm.executeUpdate("CREATE DATABASE dbname")

Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1";
Connection conn = DriverManager.getConnection(url);

Related

Correct design for querying DB through out the app

I want to use sqlite driver for my java application that I am developing with netbeans. What would be the correct "design" when it comes to integrating DB queries?
Basically should I create a static variable holding the connection which I can use to execute SQL statements through out the app? Or should I create the connection everytime I want to do the query?
Here is my code
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:Mydb.db");
st = conn.createStatement();
rs = st.executeQuery(/*My sql statement*/);
Thank you
Creating connection every time you write a query is not a good approach because it will be an overload on database for handling multiple connections.
Rather you should prefer a method or a class which would return you an instance of database connectivity.

How to store and retrieve date wise data in MySQL through java?

I want to store and retrieve data (a single column table for each date) date wise in MySQL database through java. Any suggestions on how to do it?
Any transaction with database through java is possible using JDBC library. JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connect to the database.
Here is an overview of the basic steps involved:
Registering the driver class
Class.forName("com.mysql.jdbc.Driver").newInstance();
Creating connection
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=username&password=password");
Creating statement
Write your MySQL query for storing or retrieving data from database date wise
Executing queries
Based on the query, you may get some records returned as a result or the count of rows affected
Closing connection
For more details, please refer following links:
link1
link2

How to connect two database through JDBC? Is it possible?

I have tried to connect the mysql database with the frontend with the help of JDBC driver. But i dont know how acna we implement connectivity to connect the two different databases with each other with the help of JDBC driver.
You can create two connections. One for the first database and the other for the second database. You can send commands to the first database using the first connection and you can send commands to the second database using the second connection. Your application will serve the purpose of connecting the two databases as you can select rows from one database, parse them and insert the resulting records into the other.
There is no magic in JDBC that allows you to 'connect the two database with each other'. You need to code this yourself. You create two connections, one for each database and then you write the queries and transformations to get your data from database 1 to database 2.
try (
Connection connectionToDb1 = DriverManager.getConnection(
"jdbc:firebirdsql://serverA/database1", "username", "password");
Connection connectionToDb2 = DriverManager.getConnection(
"jdbc:firebirdsql://serverB/database2", "username", "password");
Statement selectFrom1 = connectionToDb1.createStatement();
ResultSet rsFrom1 = selectFrom1.executeQuery(
"SELECT columnA, columnB FROM tableX");
PreparedStatement insertTo2 = connectionToDb2.prepareStatement(
"INSERT INTO tableY(column1, column2) VALUES (?, ?)");
) {
while (rsFrom1.next()) {
insertTo2.setString(1, rsFrom1.getString("columnA"));
insertTo2.setString(2, rsFrom1.getString("columnB"));
insertTo2.executeUpdate();
}
}
Note that this isn't a complete example: for production purposes you would disable auto commit, and use batch updates.
There are tools that can do this for you, but tool and library suggestions are off topic on SO, but I'd suggest you search for ETL (or extract, transform, load), or maybe for datapump.

How to connect to remote database through DB link using JDBC?

I need to connect to a remote database using Database link using JDBC commands.
How can it be done?
If you already have the dblink setup, you can utilize it in your SQL (sent via jdbc) by addressing the required tables like such:
select * from SCHEMA.TABLE#DBLINK_NAME
Using this query inside of your java would look something like this
public ResultSet execQuery() throws SQLException, ClassNotFoundException{
//Load the database driver
Class.forName("oracle.jdbc.OracleDriver");
//Create connection to the database
Connection myConnection = DriverManager.getConnection(connectURL,userName,userPwd);
//Create a statement link to the database for running queries
Statement myQuery = myConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//Create a resultSet to hold the returned query information
ResultSet myQueryResults = myQuery.executeQuery("select * from SCHEMA.TABLE#DBLINK_NAME");
return myQueryResults;
}
*java & oracle assumed
If you are asking about how to use JDBC to create a link between the DB you are talking to and another one, then it is "just SQL" that you (presumably) would execute in the same way as you would any other SQL statement. (If you tell us which DB you are using, we could talk about the actual SQL you need to execute.)
Otherwise, I don't think this makes sense. A DB link / Database link is a link from one database to another. But JDBC is for talking to a database from a Java client. It makes no sense (to me) to use DB link to connect a JDBC client to a database.
Please take a look at orajdbclink, on sourceforge
I am planning to to connect my oracle plsql sources to phoenix skin of hbase. It seems to me the unique way to create a connector between oracle and hbase for the moment...

Is it possible create new account in MySQL from Java?

I need use SQL without working with MySQL. Just from Java create account, databases, export and import databases. Its possible? Or I should prefer XML for it?
you can of course do it via a Statement that executes the MySQL commands for creating users:
Connection con = ...; //initialize your mysql connection
Statement s = con.createStatement();
s.execute( "CREATE USER ... " );
See the mysql page here for the syntax of create user statement.

Categories