当前位置: 技术问答>java相关
小弟是JAVA初学,关于数据库,望各位指个方向,
来源: 互联网 发布时间:2015-04-08
本文导语: 只能给这么多分了不好意思 | http://www.eng.stu.edu.cn/ebook/index.asp | 如果是初学的话,应该从java的基本功练起, java对数据库的操作已经简化了很多的了, 如果你的基本功够扎实...
只能给这么多分了不好意思
|
http://www.eng.stu.edu.cn/ebook/index.asp
|
如果是初学的话,应该从java的基本功练起,
java对数据库的操作已经简化了很多的了,
如果你的基本功够扎实的话,很容易就上手
的,但是如果你忽略了基本的东西的话,就会
陷入一些例如环境设置等的烦恼,
本立道生,只要你的根基打好了,无论要想哪个方向
发展,都会事半功倍
java对数据库的操作已经简化了很多的了,
如果你的基本功够扎实的话,很容易就上手
的,但是如果你忽略了基本的东西的话,就会
陷入一些例如环境设置等的烦恼,
本立道生,只要你的根基打好了,无论要想哪个方向
发展,都会事半功倍
|
import java.sql.*;
public class DbControl {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:xxx";
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
public DbControl() {
try {
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr);
}
catch(Exception e) {
System.err.println("DbControl(): " + e.getMessage());
}
}
public DbControl(String sConnstr) {
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnstr);
}
catch (Exception e)
{
System.err.println("DbControl(): " + e.getMessage());
}
}
public DbControl(String sDBdriver,String sConnstr) {
try
{
Class.forName(sDBdriver);
conn = DriverManager.getConnection(sConnstr);
}
catch (Exception e)
{
System.err.println("DbControl(): "+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
stmt = null;
rs=null;
try {
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
public class DbControl {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:xxx";
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
public DbControl() {
try {
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr);
}
catch(Exception e) {
System.err.println("DbControl(): " + e.getMessage());
}
}
public DbControl(String sConnstr) {
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnstr);
}
catch (Exception e)
{
System.err.println("DbControl(): " + e.getMessage());
}
}
public DbControl(String sDBdriver,String sConnstr) {
try
{
Class.forName(sDBdriver);
conn = DriverManager.getConnection(sConnstr);
}
catch (Exception e)
{
System.err.println("DbControl(): "+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
stmt = null;
rs=null;
try {
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}