import java.io.IOException
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import java.util.HashMap
import java.util.Properties
import javax.naming.Context
import javax.naming.InitialContext
import javax.sql.DataSource
import javax.sql.RowSet
import sun.jdbc.rowset.CachedRowSet
public class Orcalzx {
private static String urls,users,passwords
static
{
/* Properties prop=new Properties()
urls="jdbc:mysql://localhost:3306/invest?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false"
users="root"
passwords="123"
}
public static Connection getConnection() {
Connection con = null
try {
//con = DriverManager.getConnection(url, user, password)
con=locateDs().getConnection()//通过数据源得到连接
} catch (SQLException e) {
System.out.println(e.toString())
}
return con
}
public static DataSource locateDs()
{
DataSource ds = null
HashMap cachedDs = new HashMap()
if (cachedDs.containsKey("ds"))
ds = (DataSource)cachedDs.get("ds")
else {
try {
//初始化上下文
Context initCtx = new InitialContext()
ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/ds")//jdbc/ds是数据源名称
//ds = (DataSource)initCtx.lookup("java:/ds") //jboss数据源连接方式
cachedDs.put("ds", ds)
} catch (Exception e) {
e.printStackTrace()
}
}
return ds
}
public static RowSet query(String sql)
{
CachedRowSet crs=null//需要选择sun.jdbc.rowset.CachedRowSet
ResultSet rs=null
Connection conn=getConnection()
try {
crs=new CachedRowSet()
Statement stmt=conn.createStatement()
rs=stmt.executeQuery(sql)
crs.populate(rs)
} catch (SQLException e) {
e.printStackTrace()
}
finally{
try {
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
return crs
}
/*public static boolean preupdate(String sql)
{
boolean f=true
Connection conn=getConnection()
try {
PreparedStatement stmt=conn.prepareStatement(sql)
stmt.setInt(1, 6)
stmt.setString(2, "gfkg")
stmt.setString(3, "gflg")
//System.out.println(stmt.executeUpdate())
//stmt.executeUpdate(sql)
} catch (SQLException e) {
f=false
e.printStackTrace()
}
finally{
try {
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
return f
}*/
public static boolean update(String sql)
{
boolean f=true
Connection conn=getConnection()
try {
Statement stmt=conn.createStatement()
stmt.executeUpdate(sql)
} catch (SQLException e) {
f=false
//e.printStackTrace()
}
finally{
try {
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
return f
}
/* public static String select(String sql){
String pwd=""
try {
ResultSet rs=query(sql)//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1)//列的索引
//user=rs.getString("username")
pwd=rs.getString("dept")
System.out.println(pwd)
// System.out.println(" /密码"+password)
}
} catch (SQLException e) {
e.printStackTrace()
}
return pwd
}
public static void main(String[] args) {
// System.out.println(Orcalzx.getConnection())
String sql="select * from jtest where rownum<10"
try {
ResultSet rs=query(sql)//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1)//列的索引
int id=rs.getInt("id")
user=rs.getString("username")
password=rs.getString("password")
System.out.println("id:"+id+" /用户"+user+" /密码"+password)
}
} catch (SQLException e) {
e.printStackTrace()
}
String sql="select * from users"
try {
ResultSet rs=query(sql)//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
while(rs.next())
{
//int id=rs.getInt(1)//列的索引
user=rs.getString("username")
password=rs.getString("pwdhash")
System.out.println(" /用户"+user+" /密码"+password)
}
} catch (SQLException e) {
e.printStackTrace()
}
String sql="select * from (select rownum rid, t.* from employee t where rownum<=3)t where rid>0"
//query(sql)
ResultSet rs=query(sql)//从 CachedRowSet 对象获取数据可使用继承自 ResultSet 接口的获取方法
try {
while(rs.next())
{
//int id=rs.getInt(1)//列的索引
//user=rs.getString("username")
String pwd=rs.getString("FIRST_NAME")
System.out.println(pwd)
//System.out.println(" /密码"+password)
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}*/
}
希望你能看懂。很早以前写的。这里如何连接数据库 输出 怎么取值都有。记得采纳我的啊。
两种方式,odbc,jdbc,我只说jdbc
首先要下载一个合适的连接mysql的纯java驱动,放在jdk/jre/lib/ext文件夹下
编写程序时,第一步要加载这个驱动,因为没有驱动是没法连接的,就像电脑没有驱动是没法工作的。这样写Class.forName("com.mysql.jdbc,Driver")
然后建立连接字符串:String uri="jdbc:mysql://localhost/数据库名字"
建立连接:Connection con=DriverManager.getConnection(uri,"root","密码“)
Statement sql=con.createStatement()
ResultSet rs=sql.executeQuery("查询语句")
............
如果楼主对于这些代码不懂得话,也不要着急,刚开始跟着做就行了,慢慢就懂了
希望对你有所帮助
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)