關於mysql,sqlserverl,與oracle數據庫鏈接總結

首先準備工具類,其中須要修改的地方分別作標註java

代碼一sqlserver爲例,不一樣數據庫只需修改我所標記的第一處和第二處mysql

mysql     第一處應爲:com.mysql.jdbc.Driver   第二處url 爲:jdbc:mysql://127.0.0.1:3306/javaweb    //3306位端口,javaweb爲數據庫名web

Oracle   第一處應爲:oracle.jdbc.OracleDriver  第二處url 爲:jdbc:oracle:thin:@localhost:1522:orcl   //1522爲端口,orcl爲數據庫名sql

sql語句因數據庫不一樣略有差別,但都大同小異。數據庫

package Util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public  static Connection getConnection(){ try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); //1.此處根據數據庫修改 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } String user = "sa";        //用戶名 String password = "123456"; //密碼 String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=keji"; //2.數據庫名 Connection connection = null; try { //2 �������Ӷ���connection
             connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } return connection; } public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } } }

對數據庫操做代碼oracle

簡單的插入工具

public void add (User user){ Connection connection=DBUtil.getConnection(); String sql = "select count(*) from t_user where username = ?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { if (resultSet.getInt(1) > 0) { throw new UserException("用戶名存在") ; } } sql = "insert into t_user(username,password) values (?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getUsername()); preparedStatement.setString(2, user.getPassword()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } finally{ DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } }

刪除sqlserver

public void delete(String username) { // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection(); String sql = "delete from t_user where username = ?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, username); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); }finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } }

修改url

public void update(User user) { // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection(); //׼��sql���
        String sql = "update t_user set password = ? where username = ?"; //������䴫�����
        PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, user.getPassword()); preparedStatement.setString(2, user.getUsername()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); }finally { DBUtil.close(preparedStatement); DBUtil.close(connection); } }

查詢spa

public User load(String username) { // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection(); //׼��sql���
        String sql = "select * from t_user  where username = ?"; //������䴫�����
        PreparedStatement preparedStatement = null; ResultSet resultSet = null; User user = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, username); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { user = new User(); user.setId(resultSet.getInt("id")); user.setUsername(username); user.setBalance(resultSet.getFloat("balance")); user.setPassword(resultSet.getString("password")); user.setType(resultSet.getInt("type")); user.setCredit(resultSet.getFloat("credit")); } } catch (SQLException e) { // TODO Auto-generated catch block
 e.printStackTrace(); }finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return user; }
相關文章
相關標籤/搜索