求jdbc数据库连接的问题
就是比如说做一个页面jsp的然后有什么注册、增、删、改、查等按钮比如输入一些个人信息点注册按钮能成功注册然后数据库里有数据反正就是能和数据库就是MySQL连接、同步的应该...
就是比如说做一个页面jsp的 然后有什么注册、增、删、改、查等按钮 比如输入一些个人信息 点注册按钮 能成功注册 然后数据库里有数据 反正就是能和数据库 就是MySQL连接、同步的 应该怎么写代码?用MyEclipse做 这块不太明白 求详解 谢谢了
展开
2013-11-26
展开全部
package dao;
/**
* 数据库连接
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @author RainKylin
*
*/
public class DBConnection {
public static Connection getConnection()
{
Connection conn = null;
try {
// 加载驱动
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/webdb?user=soft?useUnicode=true&characterEncoding=utf8";
//webdb是数据库名字
// 获取数据库连接
conn = DriverManager.getConnection(url, "root", "123456");
//root是登陆数据库的名字,123456是登陆数据库的密码
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConn(ResultSet rs, PreparedStatement pstmt,Connection conn) //关闭数据库连接
{
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//前面的是数据库的链接,下面一段是用于数据库的添加、修改、删除、修改
//添加学生信息
public int insertStudent(Student stu) {
// 获取数据库连接
Connection conn = DBConnection.getConnection();
int result = 0;
PreparedStatement pstmt = null;
String sql = "insert into student values(?,?,?,?,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuId());
pstmt.setString(2, stu.getStuName());
pstmt.setInt(3, stu.getStuAge());
pstmt.setString(4, stu.getStuSex());
pstmt.setString(5, stu.getStuCollegue());
pstmt.setString(6, stu.getStuProfession());
pstmt.setString(7, stu.getStuClass());
pstmt.setString(8, stu.getStuHobby());
pstmt.setString(9, stu.getStuResume());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
}
//删除数据
public int deleteStudent(String Id)
{
// 获取数据库连接
Connection conn = DBConnection.getConnection();
int result = 0;
PreparedStatement pstmt = null;
String sql = "delete from student where stuId = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, Id);
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
//修改学生信息
public int updateStudent(Student stu) {
Connection conn = null;
int result = 0;
PreparedStatement pstmt = null;
try {
conn = DBConnection.getConnection();
String sql = "update student set stuName = ?,stuAge = ?,stuSex = ? ,stuCollegue = ?, stuProfession = ? , stuClass = ?,stuHobby = ? ,stuResume = ? where stuId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuName());
pstmt.setInt(2, stu.getStuAge());
pstmt.setString(3, stu.getStuSex());
pstmt.setString(4, stu.getStuCollegue());
pstmt.setString(5, stu.getStuProfession());
pstmt.setString(6, stu.getStuClass());
pstmt.setString(7, stu.getStuHobby());
pstmt.setString(8, stu.getStuResume());
pstmt.setString(9, stu.getStuId());
result = pstmt.executeUpdate();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
//查询所有学生
public List<Student> seleteStudent() {
Connection conn = DBConnection.getConnection();
//Vector<Vector<String>> data = new Vector<Vector<String>> ();
List<Student> students = new LinkedList<Student>();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select stuID,stuName,stuAge,stuSex,stuCollegue,stuProfession,stuClass,stuHobby,stuResume from student";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// 获取rs中数据的列数
//int col = rs.getMetaData().getColumnCount();
// 将rs的数据封装到向量中
while (rs.next()) {
Student student = new Student();
//for (int i = 1; i <= col; i++) {
student.setStuId(rs.getString(1));
student.setStuName(rs.getString(2));
student.setStuAge(rs.getInt(3));
student.setStuSex(rs.getString(4));
student.setStuCollegue(rs.getString(5));
student.setStuProfession(rs.getString(6));
student.setStuClass(rs.getString(7));
student.setStuHobby(rs.getString(8));
student.setStuResume(rs.getString(9));
//}
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
要用的时候就直接条用就OK拉
/**
* 数据库连接
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @author RainKylin
*
*/
public class DBConnection {
public static Connection getConnection()
{
Connection conn = null;
try {
// 加载驱动
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/webdb?user=soft?useUnicode=true&characterEncoding=utf8";
//webdb是数据库名字
// 获取数据库连接
conn = DriverManager.getConnection(url, "root", "123456");
//root是登陆数据库的名字,123456是登陆数据库的密码
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConn(ResultSet rs, PreparedStatement pstmt,Connection conn) //关闭数据库连接
{
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//前面的是数据库的链接,下面一段是用于数据库的添加、修改、删除、修改
//添加学生信息
public int insertStudent(Student stu) {
// 获取数据库连接
Connection conn = DBConnection.getConnection();
int result = 0;
PreparedStatement pstmt = null;
String sql = "insert into student values(?,?,?,?,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuId());
pstmt.setString(2, stu.getStuName());
pstmt.setInt(3, stu.getStuAge());
pstmt.setString(4, stu.getStuSex());
pstmt.setString(5, stu.getStuCollegue());
pstmt.setString(6, stu.getStuProfession());
pstmt.setString(7, stu.getStuClass());
pstmt.setString(8, stu.getStuHobby());
pstmt.setString(9, stu.getStuResume());
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
}
//删除数据
public int deleteStudent(String Id)
{
// 获取数据库连接
Connection conn = DBConnection.getConnection();
int result = 0;
PreparedStatement pstmt = null;
String sql = "delete from student where stuId = ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, Id);
result = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
//修改学生信息
public int updateStudent(Student stu) {
Connection conn = null;
int result = 0;
PreparedStatement pstmt = null;
try {
conn = DBConnection.getConnection();
String sql = "update student set stuName = ?,stuAge = ?,stuSex = ? ,stuCollegue = ?, stuProfession = ? , stuClass = ?,stuHobby = ? ,stuResume = ? where stuId=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, stu.getStuName());
pstmt.setInt(2, stu.getStuAge());
pstmt.setString(3, stu.getStuSex());
pstmt.setString(4, stu.getStuCollegue());
pstmt.setString(5, stu.getStuProfession());
pstmt.setString(6, stu.getStuClass());
pstmt.setString(7, stu.getStuHobby());
pstmt.setString(8, stu.getStuResume());
pstmt.setString(9, stu.getStuId());
result = pstmt.executeUpdate();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBConnection.closeConn(null, pstmt, conn);
}
// 返回数据库受影响的行数
return result;
}
//查询所有学生
public List<Student> seleteStudent() {
Connection conn = DBConnection.getConnection();
//Vector<Vector<String>> data = new Vector<Vector<String>> ();
List<Student> students = new LinkedList<Student>();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select stuID,stuName,stuAge,stuSex,stuCollegue,stuProfession,stuClass,stuHobby,stuResume from student";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// 获取rs中数据的列数
//int col = rs.getMetaData().getColumnCount();
// 将rs的数据封装到向量中
while (rs.next()) {
Student student = new Student();
//for (int i = 1; i <= col; i++) {
student.setStuId(rs.getString(1));
student.setStuName(rs.getString(2));
student.setStuAge(rs.getInt(3));
student.setStuSex(rs.getString(4));
student.setStuCollegue(rs.getString(5));
student.setStuProfession(rs.getString(6));
student.setStuClass(rs.getString(7));
student.setStuHobby(rs.getString(8));
student.setStuResume(rs.getString(9));
//}
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}
return students;
}
要用的时候就直接条用就OK拉
2013-11-26
展开全部
具体来说,你需要针对MySQL的jdbcjar驱动jar包以下方法用于创建连接Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306:test";
Connection conn = DriverManager.getConnection(url, "root", "root");jdbc:mysql://localhost:3306:test这句里面分如下解析:
jdbc:mysql:// 是指JDBC连接方式;
localhost: 是指你的本机地址;
3306 SQL数据库的端口号;
test 就是你要连接的数据库的地址
String url = "jdbc:mysql://localhost:3306:test";
Connection conn = DriverManager.getConnection(url, "root", "root");jdbc:mysql://localhost:3306:test这句里面分如下解析:
jdbc:mysql:// 是指JDBC连接方式;
localhost: 是指你的本机地址;
3306 SQL数据库的端口号;
test 就是你要连接的数据库的地址
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-26
展开全部
这块说的是数据库和逻辑代码之间的同步.主要还是连接数据库数据库连接方式(常用的):1.桥连接只是连接的驱动包和URL不一样2.直连接(很常用)eg.楼上说的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |