Export MYSQL rows into several txt files - mysql

I have a MYSQL database with 50.000 rows. Each row represents an article. I want the value of the column with they name "articletext" to be split into 50.000 files. One file for each row. I'm new to MYSQL so I'm not sure how to do this.
Can anyone help me?
Thanks

I created this small java application to solve the problem.
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Opening connection");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/articles", "username", "password");
String query = "Select title,articletext from articles";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String title = rs.getString(1);
String text = rs.getString(2);
try {
FileWriter fstream = new FileWriter(title + ".txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(text);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Closing connection");
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

And I propose my solution using Python:
import MySQLdb
def write_to_file(with_name, write_this):
with_name = str(with_name)
with open(with_name, "w") as F:
F.write(write_this)
db = MySQLdb.connect(
host="localhost", # hostname, usually localhost
user="andi", # your username
passwd="passsword", # your password
db="db_name", # name of the database
charset = "utf8", # encoding
use_unicode = True
)
cur = db.cursor()
cur.execute("select id, description from SOME_TABLE")
for row in cur.fetchall() :
write_to_file(row[0], row[1].encode('utf8'))
where row[0] will map to id, row[1] will map to description

Related

MySQL JDBC connection stop working

String url = "jdbc:mysql://localhost:3306/mysql";
String user = "root";
String pass = "root1";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, pass);
System.out.println("Connected to database");
} catch (Exception e) {
System.out.println(e);
System.out.println("Could not connect to database");
}
Password should be "root". The program does not display the message in the catch block and stops working. Can anyone tell me what happens?
[UPDATE]
I apologise I asked a bad question. The problem is already solved, Thanks. This helps to properly check whether the connection exists.
if (conn1 != null) {
System.out.println("Connected to the database test1");
}
There are three different ways to connect to SQL data base as shown in below code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class MySQLConnectExample {
public static void main(String[] args) {
// creates three different Connection objects
Connection conn1 = null;
Connection conn2 = null;
Connection conn3 = null;
try {
// connect way #1
String url1 = "jdbc:mysql://localhost:3306/test1";
String user = "root";
String password = "secret";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
System.out.println("Connected to the database test1");
}
// connect way #2
String url2 = "jdbc:mysql://localhost:3306/test2?user=root&password=secret";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
// connect way #3
String url3 = "jdbc:mysql://localhost:3306/test3";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "secret");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
}
}

how do i use select statment

I want to use select max from a table. I want to use a PreparedStatement. I have a composite primary key which consists of the t.v series and the epo number. When I add new epo it will for table and bring the t.v series code from guidline table the content of all the programs and the code for each and then add to the new table. I want it to get the last epo by getting the max and then increment +1 "an automation app".
So how can I select max where id =??
If you get me its like
pstm2=con.prepareStatement(max);
String max="select MAX(epono) as eponoo from archieve wwhere id like ? ";
This program would be helpful
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SelectRecordsUsingPreparedStatement {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:#localhost:1521:databaseName";
String username = "name";
String password = "password";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String[] args) {
ResultSet rs = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
String query = "select deptno, deptname, deptloc from dept where deptno > ?";
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setInt(1, 1001); // set input parameter
rs = pstmt.executeQuery();
// extract data from the ResultSet
while (rs.next()) {
int dbDeptNumber = rs.getInt(1);
String dbDeptName = rs.getString(2);
String dbDeptLocation = rs.getString(3);
System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

Execute Multiple JDBC Queries In a Defined Sequence

I'm develop an application to connect MySQL via JDBC. In an action, I need to execute two queries, sql to read, and sql to update.
To make sure, sql to read query would be executed first, and sql to update to be executed later, I use JDBC transaction. But somehow, the problem is, mysql execute 2nd query first, and then the first read query.
Looking for suggestions. Many thanks.
// Sql connection
Connection conn = null;
Statement stmt = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatementUpdate = null;
String sql = "SELECT item_name, item_detail FROM Order_Printing where kitchen_id = ? and order_printed = ?";
String sqlFlag = "UPDATE order_printing set order_printed = 1 where kitchen_id = ?";
try {
// Register JDBC driver (Note to add mysql connector jar file)
Class.forName("com.mysql.jdbc.Driver");
// Step 3: open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
conn.setAutoCommit(false); // Disable auto-commit mode
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setInt(1, 4);
preparedStatement.setInt(2, 0);
ResultSet rs = preparedStatement.executeQuery();
//
preparedStatementUpdate = conn.prepareStatement(sqlFlag);
preparedStatementUpdate.setInt(1, 4);
preparedStatementUpdate.executeUpdate();
int startingPos = 10;
int orderNumber = 1;
while (rs.next()) {
String item_name = "Order " + orderNumber++ + ": ";
item_name += rs.getString("item_name");
String item_detail = rs.getString("item_detail");
startingPos += 20;
g.drawString(item_name, 0, startingPos);
startingPos += 20;
g.drawString(item_detail, 0, startingPos);
}
conn.commit();
} catch (SQLException se) {
se.printStackTrace();
} catch (ClassNotFoundException se) {
se.printStackTrace();
} catch (Exception se) {
se.printStackTrace();
} finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
Try executing rs.next() while loop BEFORE you call preparedStatementUpdate.executeUpdate();

How to get data from MYSQL database

I have a database named as "test" in which I have a table named as "first" which contains raw data, I want to get this table data. What should be the prepare statement I have to use in order to get data from table "first" ? Below is the code I am trying. Any help or guidance would be appreciable.
#Path("/database") // Specific URL
#GE
#Produces(MediaType.TEXT_PLAIN)
public String returnDB_Status() throws Exception {
PreparedStatement query = null;
String result = null;
Connection conn = null;
try {
conn = mysql_prac.dbConn().getConnection(); // this works fine ...
query = conn.prepareStatement("SELECT * from first" ); // Table named as "first" is placed inside the connected database.
ResultSet rs = query.executeQuery();
result = "Data received : " + rs;
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
}
return result;
}
and the source code used get a connection
public class mysql_prac {
private static DataSource mysql_prac = null;
private static Context context = null;
public static DataSource dbConn() throws Exception {
if (mysql_prac != null) {
return mysql_prac;
}
try {
if (context == null) {
context = new InitialContext();
}
mysql_prac = (DataSource) context.lookup("JDBC_ref"); //JNDI ID (JDBC_REF)
} catch (Exception e) {
e.printStackTrace();
}
return mysql_prac;
}
}
You must loop through the ResultSet to get the fields of each row. So I made the following edit together with some comments.Please notice the comments.
try {
conn = mysql_prac.dbConn().getConnection(); // this works fine ...
query = conn.prepareStatement("SELECT * from first" ); // Table named as "first" is placed inside the connected database.
ResultSet rs = query.executeQuery();//You must loop through the results set to get the fields of each row
while(rs.next()){
String dbUserID = rs.getString("column1");//this is just an example to retrieve all data in the column called 'column1'
result = "Data received : " + dbUserID;
System.out.println(result);
}
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null)
conn.close();
}

Communications link failure: how to kill dead connections from pool?

I am facing following exception:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFException.
After specifying following pool-properties for the datasource, its able to auto-reconnect on connection-time-out; but the dead connections are existing in the pool are being reused and not being killed/evicted (ie. the validation-query and eviction parameters are not working). Following is my code. Can anyone please suggest some solution how to handle this?
public class DbConnection {
static DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
public static Connection getConnection() {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://192.168.0.9:3306/oet_v3?autoReconnect=true");
p.setDriverClassName("com.mysql.jdbc.Driver");
p.setUsername("root");
p.setPassword("napster123");
p.setJmxEnabled(true);
p.setTestWhileIdle(true);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(true);
p.setValidationInterval(20000);
p.setTimeBetweenEvictionRunsMillis(3000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setMinEvictableIdleTimeMillis(3000);
p.setMinIdle(10);
Connection con = null;
try {
Context ctx = new InitialContext();
((DataSourceProxy) datasource).setPoolProperties(p);
con = datasource.getConnection();
Statement st1 = con.createStatement();
ResultSet rs1 = st1.executeQuery("select * from User_Login limit 0,1");
if (rs1.next()) {
System.out.println("LIVE CONNECTION********con: " + con + " rs.next=" + rs1.next());
} else {
System.out.println("&&&&&rs is null so secnd conn: " + con);
DataSource datasource1 = new org.apache.tomcat.jdbc.pool.DataSource();
((DataSourceProxy) datasource1).setPoolProperties(p);
con = datasource1.getConnection();
return con;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} finally {
return con;
}
finally {
System.out.println("before returning con: "+con);
if (con!=null) try {return con;}catch (Exception ignore) {}
return con;
}
}
}