您的位置:首页 > 数据库 > MySQL

Jdbc对Mysql数据库的基本操作

2016-08-16 19:12 567 查看
jdbc对数据库的查询(无参):



<span style="font-size:14px;"> public void getQueryNoargAll(){
JdbcMysqlpool jdbc = new JdbcMysqlpool();
Connection con = null;
Statement st = null;
ResultSet rlt = null;
String sql;
try {
//获取数据库连接
con = jdbc.getConnection();
//在连接里打开一条通道createStatement,返回Statement对象
st = con.createStatement();
sql="select *from student";
//把我们想要的结果sql语句发给数据库,数据库返回的数据用java ResultSet来接受。
rlt = st.executeQuery(sql);
while (rlt.next()) {
String name=rlt.getString(1);
String interest = rlt.getString(2);
System.out.println("name:"+name+";interest:"+interest);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(rlt!=null)
rlt.close();
if(st!=null)
st.close();
if(con!=null)
con.close();
} catch (Exception e2) {
}
}
}</span>
jdbc对数据库的查询(有参):


<span style="font-size:14px;"> public void getQueryArg(String interest){
JdbcMysqlpool jdbc = new JdbcMysqlpool();
Connection con = null;
PreparedStatement pst = null;
ResultSet re = null;
String sql;
try {
//建立连接
con = jdbc.getConnection();
sql = "select name from student where interest=?";
//带参数的sql传入数据库里</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//在这里需要注意我们是有参数时,调用的是</span><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">prepareStatement</span><span style="font-size:14px;">
pst = con.prepareStatement(sql);
//设置参数的值
pst.setString(1, interest);
//执行查询,返回ResultSet结果集
re = pst.executeQuery();
while (re.next()) {
System.out.println(interest+"特别兴趣爱好的哥们"+re.getString("name"));

}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(re!=null)
re.close();
if(con!=null)
con.close();
if(pst!=null)
pst.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}</span>


jdbc调用数据库的系统函数(有参):
<span style="font-size:14px;"> //获取有参的系统函数
public void getFunctionSysarg(String abc){
JdbcMysqlpool jdbc = new JdbcMysqlpool();
CallableStatement cst = null;
Connection con = null;
ResultSet re = null;
String sql;
try {
con = jdbc.getConnection();</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//此函数的作用是将传进来的字符串全部变成大写
sql = "select UPPER(?)";
<span style="white-space:pre">	</span>//建立一个用于调用存储过程(函数)的prepareCall对象,生成CallableStatement对象,并把SQL传进去。
cst=con.prepareCall(sql);
cst.setString(1, abc);
//执行SQL,返回Resultset结果集
re=cst.executeQuery();
//查询结果集
re.next();
//打印结果集
System.out.println(re.getString(1));

} catch (Exception e) {
e.printStackTrace();
}
}</span>


jdbc调用数据库的自定义函数(有参):

public void getFuntion(int a,int b){
JdbcMysqlpool jdbc = new JdbcMysqlpool();
CallableStatement cst = null;
Connection con = null;
ResultSet re = null;
String sql;
try {
con = jdbc.getConnection();
sql = " select xiemnf_add(?,?)";
<span style="white-space:pre">			</span>//建立一个用于调用存储过程(函数)的prepareCall对象,生成CallableStatement对象,并把SQL传进去。
cst=con.prepareCall(sql);
cst.setInt(1, a);
cst.setInt(2, b);
<span style="white-space:pre">			//执行SQL,返回Resultset结果集</span>
re=cst.executeQuery();
re.next();
System.out.println(re.getInt(1));

} catch (Exception e) {
e.printStackTrace();
}finally{

try {
if(con!=null)
con.close();
if(cst!=null)
cst.close();
if(re!=null)
re.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}


jdbc调用数据库的自定义过程(有参)
<span style="font-size:14px;"> public void getProcedure(int a,int b){
JdbcMysqlpool jdbc = new JdbcMysqlpool();
Connection con = null;
CallableStatement cst = null;
String sql;
try {
con = jdbc.getConnection();
sql = "{call xiemnp_mul(?,?,?,?)}";
//建立一个用于调用存储过程(函数)的prepareCall对象,生成CallableStatement对象,并把SQL传进去。
cst = con.prepareCall(sql);
cst.setInt(1, a);
cst.setInt(2, b);
</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//设置sql返回的数据类型
cst.registerOutParameter(3, java.sql.Types.INTEGER);
cst.registerOutParameter(4, java.sql.Types.INTEGER);
cst.execute();
//执行SQL,返回Resultset结果集

//打印结果集
System.out.println(cst.getInt(3)+cst.getInt(4));

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(con!=null)
con.close();
if(cst!=null)
cst.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: