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>
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>
jdbc调用数据库的自定义函数(有参):
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;"> 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>
相关文章推荐
- JDBC链接MySql数据库基本操作
- 利用JDBC连接MySQL数据库的基本操作
- JDBC对数据表的基本操作(JDBC之二)
- JDBC的连接及基本操作
- JDBC连接Oracle数据库,以及基本的插入查询操作
- Java使用jdbc操作Mysql数据库
- jdbc操作mysql数据库
- mysql数据库基本操作总结
- MySQL数据库基本操作1
- 编写操作数据库的JAVA程序时需要的连接MySQL数据库的JDBC连接包mysql-connector-java-5.1.10.zip怎么安装
- Spring JDBC模版基本操作示例
- 通过sql语句对mysql数据库进行基本的操作
- JDBC对数据库的基本操作
- Mysql数据库基本操作概要
- Mysql数据库基本操作概要
- MySQL数据库的基本操作命令
- JDBC: 数据库基本操作演示程序
- Mysql数据库操作基本命令
- JDBC对数据库的基本操作
- JDBC应用程序常见接口API和基本操作步骤