求jdbc数据库连接的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求jdbc数据库连接的问题相关的知识,希望对你有一定的参考价值。
就是比如说做一个页面jsp的 然后有什么注册、增、删、改、查等按钮 比如输入一些个人信息 点注册按钮 能成功注册 然后数据库里有数据 反正就是能和数据库 就是mysql连接、同步的 应该怎么写代码?用MyEclipse做 这块不太明白 求详解 谢谢了
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拉 参考技术A 具体来说,你需要针对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 就是你要连接的数据库的地址 参考技术B 这块说的是数据库和逻辑代码之间的同步.主要还是连接数据库数据库连接方式(常用的):1.桥连接只是连接的驱动包和URL不一样2.直连接(很常用)eg.楼上说的
jdbc连接数据库的代码问题jdbc连接mysql数据库
参考技术A 用这个类吧.好的话,给我加加分.import java.sql.*;
/**
* @功能: 一个JDBC的本地化API连接类,封装了数据操作方法,只用传一个SQL语句即可
* @作者: 李开欢
* @日期: 2007/
*/
public class ConnectionDemo
/*
* 这里可以将常量全部放入另一个类中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo()
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
public static Connection getConnection()
System.out.println("连接中...");
try
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功连接");
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
return conn;
public static Statement getStatement(String sql)
System.out.println("执行SQL语句中...");
try
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select"))
rs = ps.executeQuery(sql);
System.out.println("执行完查询操作,结果已返回ResultSet集合");
else if(sql.substring(0, 6).equals("delete"))
ps.executeUpdate(sql);
System.out.println("已执行完毕删除操作");
else if(sql.substring(0, 6).equals("insert"))
ps.executeUpdate(sql);
System.out.println("已执行完毕增加操作");
else
ps.executeUpdate(sql);
System.out.println("已执行完毕更新操作");
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
return ps;
public static ResultSet getResultSet()
System.out.println("查询结果为:");
return rs;
public static void closeConnection()
System.out.println("关闭连接中...");
try
if (rs != null)
rs.close();
System.out.println("已关闭ResultSet");
if (ps != null)
ps.close();
System.out.println("已关闭Statement");
if (conn != null)
conn.close();
System.out.println("已关闭Connection");
catch (Exception e)
// TODO: handle exception
public static void main(String[] args)
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教学设备')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("编号 "+"类 型");
try
while(rs.next())
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
ConnectionDemo.closeConnection();
参考技术B 连接mysql的完整代码如下:
String dbURL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
String username = "root";
String password = "";
try
Class.forName("com.mysql.jdbc.Driver");
catch(ClassNotFoundException e)
try
conn = DriverManager.getConnection(dbURL, username, password);
String sql = "INSERT INTO goods VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "0001");
pstmt.setString(2, "苹果手机");
pstmt.setString(3, "电子类");
pstmt.setInt(4, 6800);
pstmt.setString(3, "李某某");
pstmt.executeUpdate();
catch (SQLException ex)
ex.printStackTrace();
以上是关于求jdbc数据库连接的问题的主要内容,如果未能解决你的问题,请参考以下文章
用jdbc连接oracle数据库,现在要实现数据的更新操作,在java web 项目里,求大神简单讲解下,再给出点代码
本人初学java,学到JDBC那块需要连接Mysql数据库但是不知道如何配置mysql驱动程序?求帮忙配置一下。