java连接数据库mysql代码及简单访问数据库 30

 我来答
千锋教育
2016-01-02 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
import java.sql.*;
public class DataBasePractice {

public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/mydata";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "root";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from student";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名");
System.out.println("-----------------");

String name = null;
String id = null;
while(rs.next()){
//获取stuname这列数据
name = rs.getString("stuname");
//获取stuid这列数据
id = rs.getString("stuid");
//首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
//然后使用GB2312字符集解码指定的字节数组。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//输出结果
System.out.println(id + "\t" + name);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}

}

在上面while代码段后面添加以下代码段:
String name = null;

String id = null;
while(rs.next()){
//获取stuname这列数据
name = rs.getString("stuname");
//获取stuid这列数据
id = rs.getString("stuid");
//首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
//然后使用GB2312字符集解码指定的字节数组。
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
//输出结果
System.out.println(id + "\t" + name);
}

PreparedStatement psql;
ResultSet res;
//预处理添加数据,其中有两个参数--“?”
psql = con.prepareStatement("insert into student values(?,?)");
psql.setInt(1, 8); //设置参数1,创建id为5的数据
psql.setString(2, "xiaogang"); //设置参数2,name 为小明
psql.executeUpdate(); //执行更新

//预处理更新(修改)数据
psql = con.prepareStatement("update student set stuname = ? where stuid = ?");
psql.setString(1,"xiaowang"); //设置参数1,将name改为王五
psql.setInt(2,10); //设置参数2,将id为2的数据做修改
psql.executeUpdate();

//预处理删除数据
psql = con.prepareStatement("delete from student where stuid = ?");
psql.setInt(1, 5);
psql.executeUpdate();

//查询修改数据后student表中的数据
psql = con.prepareStatement("select*from student");
res = psql.executeQuery(); //执行预处理sql语句
System.out.println("执行增加、修改、删除后的数据");
while(res.next()){
name = res.getString("stuname");
id = res.getString("stuid");
name = new String(name.getBytes("ISO-8859-1"),"gb2312");
System.out.println(id + "\t" + name);
}
res.close();
psql.close();
sun_wxb
推荐于2018-02-27 · TA获得超过264个赞
知道小有建树答主
回答量:648
采纳率:0%
帮助的人:468万
展开全部
import java.sql.*;

//封装数据库对象,使用javaBean;
public class DB {
public static Connection getConnection() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/db?user=root&password=root";
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStatement(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static int executeUpdate(Connection conn, String sql) {
int row = 0;
Statement stmt = null;
try {
stmt = conn.createStatement();
row = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// close();
close(stmt);
}
return row;
}
public static PreparedStatement prepareStatement(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepareStatement(Connection conn,
String sql, int autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(PreparedStatement pstmt) {//可以使用close(Statement stmt)方法,父类引用指向子类对象;
if (null != pstmt) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
pstmt = null;
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
stmt = null;
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
rs = null;
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
conn = null;
}
}

//////////////////
<%
String title = request.getParameter("title");
String context = request.getParameter("context");
//System.out.println(int_id+"-"+title+"-"+context);
//Connection conn = DB.getConnection();
boolean defaultAutoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
//String _sql = "update article set title = ?,cont = ?,pdate = now() where id = ?";
String _sql = "update article set title = ?,cont = ? where id = ?";
PreparedStatement pstmt = DB.prepareStatement(conn, _sql);
pstmt.setString(1, title);
pstmt.setString(2, context);
pstmt.setInt(3, int_id);
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(defaultAutoCommit);
DB.close(pstmt);
DB.close(conn);
response.sendRedirect(url);

%>
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式