您的位置首页生活快答

用java方式实现与主流数据库mysql通信

用java方式实现与主流数据库mysql通信

的有关信息介绍如下:

用java方式实现与主流数据库mysql通信

String sql=“select * from cq_user where name like "uuu”;

Class.forName("com.mysql.jdbc.Driver");//加载驱动

Connection conn = DriverManager.getConnection([数据库地址和库名], [用户名], [密码]);//获取连接

Statement statement = conn.createStatement();获得查询对象,这个不止一种,按你需要的来

ResultSet rs = stmt.executeQuery(sql);//获取结果集对象

String xxx = rs.getString("name");//从结果集对象取数据,如果是其他类型的选择对应的方法

rs.close();

statement.close();

conn.close();

大致过程就这样,方便你看懂。异常处理我就不加了,按你需要来吧(三大对象关闭放finally里)。

package com.hd.jdbc;

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();

}

}*/

}

希望你能看懂。很早以前写的。这里如何连接数据库 输出 怎么取值都有。记得采纳我的啊。