java数据库编程
来源:互联网 发布:自称老夫 知乎 编辑:程序博客网 时间:2024/06/05 17:08
package DB;
import java.sql.*;
import java.util.*;
public class dbop
{
public final static StringDriverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static String url =null;
public static String username =null;
public static String password =null;
public dbop(String usernamepame,StringPasswordpame,String urlpame)
{
super();
username = usernamepame;
password = Passwordpame;
url = urlpame;
}
/**
* @创建数据库的连接
* @return 返回conn
*/
public static Connection getConnection()
{
Connection conn = null;
try
{
Class.forName(DriverName).newInstance();//实例化数据库驱动
conn =DriverManager.getConnection(url,username,password);//连接数据库
System.out.print("连接数据库成功!");
}
catch (InstantiationException e)
{
System.out.println("驱动实例化失败!");
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
System.out.println("加载类时发生异常!");
e.printStackTrace();
}
catch (SQLException e)
{
System.out.println("数据库连接异常!");
e.printStackTrace();
}
return conn;
}
/**
*@创建数据库查询语句并执行
*@return 返回ArrayList查询结果
*/
public static ArrayList query(String SQL)
{
ArrayList arrayList =null;
Connection conn = null;
conn = getConnection();
try
{
Statement stmt =conn.createStatement();//创建数据库查询语句
ResultSet rs =stmt.executeQuery(SQL);//执行查询语句并返回rs
int colnum =rs.getMetaData().getColumnCount();//记录返回表的列数
if(rs!=null&&colnum!=0)//查询结果不为空
{
arrayList = newArrayList();
ArrayList tempAR = newArrayList();
while(rs.next())
{
for(int i =0;i <= colnum;i++)
{
tempAR.add(rs.getObject(i));
}
arrayList.add(tempAR);
}
}
else
{
System.out.print("查询结果为空");//查询结果为空
}
conn.close();
stmt.close();
rs.close();
}
catch(SQLException e)
{
System.out.print("查询失败!");
e.printStackTrace();
}
return arrayList;
}
/**
* @数据库查更新和删除操作
* @return 返回所影响的行数
*/
public static intupdata(String updataSQL)
{
int rownum =0;
try
{
Connection conn = getConnection();
Statement stmt = conn.createStatement();
rownum = stmt.executeUpdate(updataSQL);
conn.close();
stmt.close();
}
catch(SQLException e)
{
System.out.print("数据库更新或删除操作时,创建执行语句失败!");
e.printStackTrace();
}
return rownum;
}
}
本类只适用与SQLserver2000,经测试通过!
- Java 数据库编程 - 连接数据库
- java数据库编程--MySQL数据库
- Java数据库编程简略
- Java数据库编程简略
- Java数据库编程
- java数据库编程过程
- JDBC Java数据库编程
- JAVA数据库编程
- 19.Java 数据库编程
- java 数据库编程
- java数据库编程
- java数据库编程
- Java数据库编程---JDBC
- Java数据库编程---Clob
- Java数据库编程---Blob
- Java数据库编程---事务处理
- Java数据库编程
- java 数据库编程
- c#调用系统DLL
- wince编程常用控制窗体API
- windows编程常用消息汇总
- 常用键盘键值
- JSP开发环境的建立
- java数据库编程
- POM详解
- JSP--第一个练习
- 学习摘抄
- 无聊之无欲篇
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- Problem H: 蚂蚁的最短行径
- 为了我们的理想
- 深入理解linux系统的目录结构