java与数据库
java和数据库的连接,我们得先下一个包是专门用来与数据库连接的驱动还有方法,这边我们将此方法封装,然后这边的思路:加载驱动,进行连接(connection),创建向数据库发送命令的对象.
public static Connection getconnection() {// 加载驱动 且进行连接
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/person?useSSL=false";
connection=(Connection) DriverManager.getConnection(url, "root", "1131457166@qq.com");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static PreparedStatement getPreparedStatement(Connection con,String sql) {//设置发送sql 的方法
PreparedStatement pre=null;
try {
pre=(PreparedStatement) con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pre ;
}
package shuju;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.lang.reflect.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Field;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSetMetaData;
/**
* 将数据库的内容以键值对的关系存入集合里
*/
public class Jdbc22 {
public static void main(String[] args) {
Connection con=Study_1.getconnection();
String sql="select * from student";
PreparedStatement p=Study_1.getPreparedStatement(con, sql);
try {
ResultSet re=p.executeQuery();//执行sql语句 并返回resultset对象
ResultSetMetaData rest=(ResultSetMetaData) re.getMetaData();//获得结果集里的列的信息
int count=rest.getColumnCount();//得到列的个数
Map<String, Object> map=new HashMap();//map 集合
List< Map<String, Object>> list= new ArrayList<>();//list集合
/*
* 先把结果存到map中
*/
while(re.next()) {// 会有个光标对象然后进行遍历
for(int i=1;i<count;i++) {
String name=rest.getColumnName(i);// 得到当前字段的名字
Object value=re.getObject(name);//得到当前字段下对应的值
map.put(name, value);//将值放入map中
}
list.add(map);// 将map放入集合中
}
Iterator it=list.iterator();//创建迭代器的对象
while(it.hasNext()) {//list里全是map 对象所以不能直接遍历出来
Map<String, Object> ma=(Map<String, Object>) it.next();//得到list里的map
Set<Map.Entry<String, Object>> set=(Set<Entry<String, Object>>) ma.entrySet();//set里是entry对象
Iterator i=set.iterator();
while(i.hasNext()) {
Entry a=(Entry)i.next();
System.out.println(a.getKey()+" value: "+a.getValue());
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
Study_1.close(p, con);
}
}
}
import java.sql.SQLException;
import java.util.Set;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class Jdbc1 {
public static void main(String[] args) {
inyo();
yyp();
}
public static void inyo() {
Connection con=Study_1.getconnection();
Long time=System.currentTimeMillis();
String sql="insert into teacher (Tid,tname) values(?,?)";
PreparedStatement sta=null;
try {
sta=(PreparedStatement) con.prepareStatement(sql);
sta.setInt(1, 903);
sta.setString(2, "王五");
sta.addBatch();//将一组参数添加到PreparedStatement 中
sta.setInt(1, 904);
sta.setString(2, "王老无");
sta.addBatch();
sta.setInt(1, 902);
sta.setString(2, "王老期");
sta.addBatch();
int [] a=sta.executeBatch();//执行一组参数若大于0则执行成功
Long time1=System.currentTimeMillis();
System.out.println(time1-time);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Study_1.close(sta, con);
}
}
public static void yyp() {
Connection con=Study_1.getconnection();
Long time=System.currentTimeMillis();
String sql="insert into teacher (Tid,tname) values(906,'网1')";
String sql1="insert into teacher (Tid,tname) values(908,'网2')";
String sql2="insert into teacher (Tid,tname) values(909,'网3')";
String sql3="insert into teacher (Tid,tname) values(910,'网4')";
System.out.println(sql);
try {
Statement sta=(Statement) con.createStatement();
sta.addBatch(sql);
sta.addBatch(sql2);
sta.addBatch(sql1);
sta.addBatch(sql3);
int[] a=sta.executeBatch();
Long time1=System.currentTimeMillis();
System.out.println(time1-time);
for(int b: a) {
System.out.println(b);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.lang.reflect.*;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Field;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSetMetaData;
//调用无参的存储过程
public class Jdbc22 {
public static void main(String[] args) {
Connection con=Study_1.getconnection();
/*String sql="call tt()";//无参的存储过程
CallableStatement call=null;
try {
call = (CallableStatement) con.prepareCall(sql);
ResultSet set=call.executeQuery();
while(set.next()) {
System.out.println(set.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
call.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//调用in的有参数存储过程
/*String sql="call tt1(?)";
CallableStatement call=null;
try {
call=(CallableStatement) con.prepareCall(sql);
call.setInt(1,555 );
ResultSet rs=call.executeQuery();
while(rs.next()) {
System.out.println("5555");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
call.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//out 参数的存储过程
/*String sql="call tt2(?)";
CallableStatement call=null;
try {
call=(CallableStatement) con.prepareCall(sql);
call.registerOutParameter(1,Types.INTEGER);
call.executeQuery();
int a = call.getInt(1);//接受返回的值
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
call.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
//inout 存入存储过程
String sql="call tt3(?)";
try {
CallableStatement call=(CallableStatement) con.prepareCall(sql);
call.setInt(1, 233);
call.registerOutParameter(1, Types.INTEGER);//按顺序位置 parameterIndex 将 OUT 参数注册为 JDBC 类型 sqlType。
ResultSet re=call.executeQuery();//执行SQl 语句
int a=call.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
上诉代码是一些基本的用法,仅供参考。