Jython using JDBC

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I want to access databases by Jython by using JDBC drivers. I started with SQLite because this needs no database installation. I found two tutorials that implement similar things in Java and in Python using the same sample database.
- SQLite Python samples
- SQLite Java samples
I translated the following Java program to Jython:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
public static void main(String args) throws ClassNotFoundException
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
catch(SQLException e)
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
finally
try
if(connection != null)
connection.close();
catch(SQLException e)
// connection close failed.
System.err.println(e);
Here is my implementation of this Java program in Jython:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
#load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
url = "jdbc:sqlite:C:/sqlite/db/chinook.db"
connection = None
try:
connection = java.sql.DriverManager.getConnection(url)
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
rs = statement.executeQuery("select * from person")
while (rs.next()):
print("name = %s"%(rs.getString("name")))
print("id = %s"%(rs.getString("id")))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s"%e.getMessage())
finally:
try:
if(connection is not None):
connection.close()
except SQLException as e:
# connection close failed.
print("error: %s"%e.getMessage())
Is this an appropriate translation from Java to Jython? Can the Jython program be improved?
python jdbc jython
add a comment |Â
up vote
2
down vote
favorite
I want to access databases by Jython by using JDBC drivers. I started with SQLite because this needs no database installation. I found two tutorials that implement similar things in Java and in Python using the same sample database.
- SQLite Python samples
- SQLite Java samples
I translated the following Java program to Jython:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
public static void main(String args) throws ClassNotFoundException
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
catch(SQLException e)
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
finally
try
if(connection != null)
connection.close();
catch(SQLException e)
// connection close failed.
System.err.println(e);
Here is my implementation of this Java program in Jython:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
#load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
url = "jdbc:sqlite:C:/sqlite/db/chinook.db"
connection = None
try:
connection = java.sql.DriverManager.getConnection(url)
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
rs = statement.executeQuery("select * from person")
while (rs.next()):
print("name = %s"%(rs.getString("name")))
print("id = %s"%(rs.getString("id")))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s"%e.getMessage())
finally:
try:
if(connection is not None):
connection.close()
except SQLException as e:
# connection close failed.
print("error: %s"%e.getMessage())
Is this an appropriate translation from Java to Jython? Can the Jython program be improved?
python jdbc jython
1
Not versed into Jython, but as far as I understand you should be able to use thesqlitemodule from Python directly and benefit from the context managers already in place.
â Mathias Ettinger
Feb 18 at 19:52
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to access databases by Jython by using JDBC drivers. I started with SQLite because this needs no database installation. I found two tutorials that implement similar things in Java and in Python using the same sample database.
- SQLite Python samples
- SQLite Java samples
I translated the following Java program to Jython:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
public static void main(String args) throws ClassNotFoundException
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
catch(SQLException e)
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
finally
try
if(connection != null)
connection.close();
catch(SQLException e)
// connection close failed.
System.err.println(e);
Here is my implementation of this Java program in Jython:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
#load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
url = "jdbc:sqlite:C:/sqlite/db/chinook.db"
connection = None
try:
connection = java.sql.DriverManager.getConnection(url)
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
rs = statement.executeQuery("select * from person")
while (rs.next()):
print("name = %s"%(rs.getString("name")))
print("id = %s"%(rs.getString("id")))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s"%e.getMessage())
finally:
try:
if(connection is not None):
connection.close()
except SQLException as e:
# connection close failed.
print("error: %s"%e.getMessage())
Is this an appropriate translation from Java to Jython? Can the Jython program be improved?
python jdbc jython
I want to access databases by Jython by using JDBC drivers. I started with SQLite because this needs no database installation. I found two tutorials that implement similar things in Java and in Python using the same sample database.
- SQLite Python samples
- SQLite Java samples
I translated the following Java program to Jython:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample
public static void main(String args) throws ClassNotFoundException
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
catch(SQLException e)
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
finally
try
if(connection != null)
connection.close();
catch(SQLException e)
// connection close failed.
System.err.println(e);
Here is my implementation of this Java program in Jython:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
#load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
url = "jdbc:sqlite:C:/sqlite/db/chinook.db"
connection = None
try:
connection = java.sql.DriverManager.getConnection(url)
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
rs = statement.executeQuery("select * from person")
while (rs.next()):
print("name = %s"%(rs.getString("name")))
print("id = %s"%(rs.getString("id")))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s"%e.getMessage())
finally:
try:
if(connection is not None):
connection.close()
except SQLException as e:
# connection close failed.
print("error: %s"%e.getMessage())
Is this an appropriate translation from Java to Jython? Can the Jython program be improved?
python jdbc jython
edited Feb 18 at 12:03
200_success
123k14143401
123k14143401
asked Jan 19 at 1:35
miracle173
1,014514
1,014514
1
Not versed into Jython, but as far as I understand you should be able to use thesqlitemodule from Python directly and benefit from the context managers already in place.
â Mathias Ettinger
Feb 18 at 19:52
add a comment |Â
1
Not versed into Jython, but as far as I understand you should be able to use thesqlitemodule from Python directly and benefit from the context managers already in place.
â Mathias Ettinger
Feb 18 at 19:52
1
1
Not versed into Jython, but as far as I understand you should be able to use the
sqlite module from Python directly and benefit from the context managers already in place.â Mathias Ettinger
Feb 18 at 19:52
Not versed into Jython, but as far as I understand you should be able to use the
sqlite module from Python directly and benefit from the context managers already in place.â Mathias Ettinger
Feb 18 at 19:52
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
It's fine for a single script. Python has first class functions so you can create a decorator or a some utility code to abstract out the try catch finally.
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
add a comment |Â
up vote
1
down vote
The translation is very faithful, testament to how similar language
constructs really are ... in any case there are a lot of ways that this
would probably be better in production code.
- Foremost, take a look at the typical style guidelines for Python
programs, PEP8, not to
follow them slavishly, but to get a good sense of what other readers
will typically expect a Python program to look like. - For example, there are several unnecessary parenthesis sprinkled in
throughout the code, I'd also typically expect an empty line after the
imports. Also, it's Python, you don't need semicolons at the end of
lines. - In a production script you'd want to pass in the used database file.
And constants should follow a different naming convention than regular
variables, usually uppercase, that is,URL. - Also you most likely want a
mainfunction that's not invoked when
the file is used as a module. That's just good practice. x is not Nonecan very likely more easily be written asxbecauseNoneis also a false value (sox is not Falsebecomesx is True
becomes simplyx).- But also
try/finallycombination in Java (which coincidentally has a
better way of writing it astry (Connection c = ...) ...too)
could be written better in Python withwith ... as connection:
which will take care of the freeing up of the resource. However
in Jython it seems you needcontextlib.closingas a wrapper to
achieve what Java callsAutoCloseable. Meaning it would bewith closing(...) as connection:basically. - I wouldn't necessarily catch the exceptions since the backtrace
provides lots of valuable context, but then again that all depends on
how the script is intended to be used.
Apart from that looks fine.
Looks like this:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
from contextlib import closing
URL = "jdbc:sqlite:herp.db"
def main():
try:
with closing(java.sql.DriverManager.getConnection(URL)) as connection:
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)")
statement.executeUpdate("insert into person values(1, 'leo')")
statement.executeUpdate("insert into person values(2, 'yui')")
rs = statement.executeQuery("select * from person")
while rs.next():
print("name = %s" % rs.getString("name"))
print("id = %s" % rs.getString("id"))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s" % e.getMessage())
if __name__ == "__main__":
# load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
main()
Oh yeah that's actually quite a good point: Python, and most likely Jython as well, has native modules available for a lot of things, meaning if you don't need to use Java specific libraries, perhaps you can get away with using those and their more "native" APIs.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It's fine for a single script. Python has first class functions so you can create a decorator or a some utility code to abstract out the try catch finally.
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
add a comment |Â
up vote
1
down vote
It's fine for a single script. Python has first class functions so you can create a decorator or a some utility code to abstract out the try catch finally.
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
add a comment |Â
up vote
1
down vote
up vote
1
down vote
It's fine for a single script. Python has first class functions so you can create a decorator or a some utility code to abstract out the try catch finally.
It's fine for a single script. Python has first class functions so you can create a decorator or a some utility code to abstract out the try catch finally.
answered Jan 19 at 3:32
Steven Lewis
111
111
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
add a comment |Â
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
Thank you for your comment. I have a very basic knowledge of decorators but I don't understand what you mean by 'abstract out the try catch finally' and what I should try to achieve. Could you elaborate on this?
â miracle173
Jan 19 at 7:01
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
The idea is to separate out the interesting parts of your code (the SQL, etc) from the boring plumbing stuff (the try catch block). Think about how you might do that.
â Steven Lewis
Jan 20 at 14:11
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
codeproject.com/Questions/172028/How-to-refactor-try-catch
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
Something like the link I posted above, which is in java but the idea is the same.
â Steven Lewis
Jan 20 at 14:16
add a comment |Â
up vote
1
down vote
The translation is very faithful, testament to how similar language
constructs really are ... in any case there are a lot of ways that this
would probably be better in production code.
- Foremost, take a look at the typical style guidelines for Python
programs, PEP8, not to
follow them slavishly, but to get a good sense of what other readers
will typically expect a Python program to look like. - For example, there are several unnecessary parenthesis sprinkled in
throughout the code, I'd also typically expect an empty line after the
imports. Also, it's Python, you don't need semicolons at the end of
lines. - In a production script you'd want to pass in the used database file.
And constants should follow a different naming convention than regular
variables, usually uppercase, that is,URL. - Also you most likely want a
mainfunction that's not invoked when
the file is used as a module. That's just good practice. x is not Nonecan very likely more easily be written asxbecauseNoneis also a false value (sox is not Falsebecomesx is True
becomes simplyx).- But also
try/finallycombination in Java (which coincidentally has a
better way of writing it astry (Connection c = ...) ...too)
could be written better in Python withwith ... as connection:
which will take care of the freeing up of the resource. However
in Jython it seems you needcontextlib.closingas a wrapper to
achieve what Java callsAutoCloseable. Meaning it would bewith closing(...) as connection:basically. - I wouldn't necessarily catch the exceptions since the backtrace
provides lots of valuable context, but then again that all depends on
how the script is intended to be used.
Apart from that looks fine.
Looks like this:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
from contextlib import closing
URL = "jdbc:sqlite:herp.db"
def main():
try:
with closing(java.sql.DriverManager.getConnection(URL)) as connection:
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)")
statement.executeUpdate("insert into person values(1, 'leo')")
statement.executeUpdate("insert into person values(2, 'yui')")
rs = statement.executeQuery("select * from person")
while rs.next():
print("name = %s" % rs.getString("name"))
print("id = %s" % rs.getString("id"))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s" % e.getMessage())
if __name__ == "__main__":
# load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
main()
Oh yeah that's actually quite a good point: Python, and most likely Jython as well, has native modules available for a lot of things, meaning if you don't need to use Java specific libraries, perhaps you can get away with using those and their more "native" APIs.
add a comment |Â
up vote
1
down vote
The translation is very faithful, testament to how similar language
constructs really are ... in any case there are a lot of ways that this
would probably be better in production code.
- Foremost, take a look at the typical style guidelines for Python
programs, PEP8, not to
follow them slavishly, but to get a good sense of what other readers
will typically expect a Python program to look like. - For example, there are several unnecessary parenthesis sprinkled in
throughout the code, I'd also typically expect an empty line after the
imports. Also, it's Python, you don't need semicolons at the end of
lines. - In a production script you'd want to pass in the used database file.
And constants should follow a different naming convention than regular
variables, usually uppercase, that is,URL. - Also you most likely want a
mainfunction that's not invoked when
the file is used as a module. That's just good practice. x is not Nonecan very likely more easily be written asxbecauseNoneis also a false value (sox is not Falsebecomesx is True
becomes simplyx).- But also
try/finallycombination in Java (which coincidentally has a
better way of writing it astry (Connection c = ...) ...too)
could be written better in Python withwith ... as connection:
which will take care of the freeing up of the resource. However
in Jython it seems you needcontextlib.closingas a wrapper to
achieve what Java callsAutoCloseable. Meaning it would bewith closing(...) as connection:basically. - I wouldn't necessarily catch the exceptions since the backtrace
provides lots of valuable context, but then again that all depends on
how the script is intended to be used.
Apart from that looks fine.
Looks like this:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
from contextlib import closing
URL = "jdbc:sqlite:herp.db"
def main():
try:
with closing(java.sql.DriverManager.getConnection(URL)) as connection:
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)")
statement.executeUpdate("insert into person values(1, 'leo')")
statement.executeUpdate("insert into person values(2, 'yui')")
rs = statement.executeQuery("select * from person")
while rs.next():
print("name = %s" % rs.getString("name"))
print("id = %s" % rs.getString("id"))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s" % e.getMessage())
if __name__ == "__main__":
# load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
main()
Oh yeah that's actually quite a good point: Python, and most likely Jython as well, has native modules available for a lot of things, meaning if you don't need to use Java specific libraries, perhaps you can get away with using those and their more "native" APIs.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The translation is very faithful, testament to how similar language
constructs really are ... in any case there are a lot of ways that this
would probably be better in production code.
- Foremost, take a look at the typical style guidelines for Python
programs, PEP8, not to
follow them slavishly, but to get a good sense of what other readers
will typically expect a Python program to look like. - For example, there are several unnecessary parenthesis sprinkled in
throughout the code, I'd also typically expect an empty line after the
imports. Also, it's Python, you don't need semicolons at the end of
lines. - In a production script you'd want to pass in the used database file.
And constants should follow a different naming convention than regular
variables, usually uppercase, that is,URL. - Also you most likely want a
mainfunction that's not invoked when
the file is used as a module. That's just good practice. x is not Nonecan very likely more easily be written asxbecauseNoneis also a false value (sox is not Falsebecomesx is True
becomes simplyx).- But also
try/finallycombination in Java (which coincidentally has a
better way of writing it astry (Connection c = ...) ...too)
could be written better in Python withwith ... as connection:
which will take care of the freeing up of the resource. However
in Jython it seems you needcontextlib.closingas a wrapper to
achieve what Java callsAutoCloseable. Meaning it would bewith closing(...) as connection:basically. - I wouldn't necessarily catch the exceptions since the backtrace
provides lots of valuable context, but then again that all depends on
how the script is intended to be used.
Apart from that looks fine.
Looks like this:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
from contextlib import closing
URL = "jdbc:sqlite:herp.db"
def main():
try:
with closing(java.sql.DriverManager.getConnection(URL)) as connection:
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)")
statement.executeUpdate("insert into person values(1, 'leo')")
statement.executeUpdate("insert into person values(2, 'yui')")
rs = statement.executeQuery("select * from person")
while rs.next():
print("name = %s" % rs.getString("name"))
print("id = %s" % rs.getString("id"))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s" % e.getMessage())
if __name__ == "__main__":
# load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
main()
Oh yeah that's actually quite a good point: Python, and most likely Jython as well, has native modules available for a lot of things, meaning if you don't need to use Java specific libraries, perhaps you can get away with using those and their more "native" APIs.
The translation is very faithful, testament to how similar language
constructs really are ... in any case there are a lot of ways that this
would probably be better in production code.
- Foremost, take a look at the typical style guidelines for Python
programs, PEP8, not to
follow them slavishly, but to get a good sense of what other readers
will typically expect a Python program to look like. - For example, there are several unnecessary parenthesis sprinkled in
throughout the code, I'd also typically expect an empty line after the
imports. Also, it's Python, you don't need semicolons at the end of
lines. - In a production script you'd want to pass in the used database file.
And constants should follow a different naming convention than regular
variables, usually uppercase, that is,URL. - Also you most likely want a
mainfunction that's not invoked when
the file is used as a module. That's just good practice. x is not Nonecan very likely more easily be written asxbecauseNoneis also a false value (sox is not Falsebecomesx is True
becomes simplyx).- But also
try/finallycombination in Java (which coincidentally has a
better way of writing it astry (Connection c = ...) ...too)
could be written better in Python withwith ... as connection:
which will take care of the freeing up of the resource. However
in Jython it seems you needcontextlib.closingas a wrapper to
achieve what Java callsAutoCloseable. Meaning it would bewith closing(...) as connection:basically. - I wouldn't necessarily catch the exceptions since the backtrace
provides lots of valuable context, but then again that all depends on
how the script is intended to be used.
Apart from that looks fine.
Looks like this:
# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
# C:jython2.7.0binjython.exe sample.py
import java
from java.sql import SQLException
from contextlib import closing
URL = "jdbc:sqlite:herp.db"
def main():
try:
with closing(java.sql.DriverManager.getConnection(URL)) as connection:
statement = connection.createStatement()
statement.executeUpdate("drop table if exists person")
statement.executeUpdate("create table person (id integer, name string)")
statement.executeUpdate("insert into person values(1, 'leo')")
statement.executeUpdate("insert into person values(2, 'yui')")
rs = statement.executeQuery("select * from person")
while rs.next():
print("name = %s" % rs.getString("name"))
print("id = %s" % rs.getString("id"))
except SQLException as e:
# if the error message is "out of memory",
# it probably means no database file is found
print("error: %s" % e.getMessage())
if __name__ == "__main__":
# load the sqlite-JDBC driver using the current class loader
java.lang.Class.forName("org.sqlite.JDBC")
main()
Oh yeah that's actually quite a good point: Python, and most likely Jython as well, has native modules available for a lot of things, meaning if you don't need to use Java specific libraries, perhaps you can get away with using those and their more "native" APIs.
answered Jul 18 at 22:28
ferada
8,8761453
8,8761453
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185441%2fjython-using-jdbc%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Not versed into Jython, but as far as I understand you should be able to use the
sqlitemodule from Python directly and benefit from the context managers already in place.â Mathias Ettinger
Feb 18 at 19:52