zoukankan      html  css  js  c++  java
  • Java学习---连接数据库操作

    Java连接Oracle数据库

     1 package com.ftl.mysql;
     2 import java.sql.Connection;
     3 import java.sql.DriverManager;
     4 import java.sql.PreparedStatement;
     5 import java.sql.ResultSet;
     6 
     7 public class HelloFtl {
     8     public static final String DBDRIVER= "oracle.jdbc.driver.OracleDriver";
     9     public static final String DBURL= "jdbc:oracle:thin:@localhost:1521:impdb";
    10     public static final String DBUSER= "test";
    11     public static final String DBPASS= "Ch_123";
    12     
    13     public static void main(String[] args) throws Exception  {
    14         System.out.println("--------------------------------------");
    15             Connection conn = null;
    16             PreparedStatement pstmt = null;
    17             ResultSet rs = null;
    18             String sql = null;
    19             Class.forName(DBDRIVER);
    20             conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS) ;
    21 //            sql = "select * from teachers ";
    22             sql = "select tname from teachers where tid = ? and score = ?";
    23             int tid = 1;
    24             int score = 100;
    25             String name = null;
    26             pstmt = conn.prepareStatement(sql);
    27             pstmt.setInt(1, tid);
    28             pstmt.setInt(2, score);
    29             rs = pstmt.executeQuery();
    30             while(rs.next())
    31             {
    32 //                int tid = rs.getInt(1);
    33 //                String tname = rs.getString(2);
    34 //                String sex = rs.getString(3);
    35 //                float score = rs.getFloat(4);
    36 //                System.out.println("Tid: " +  tid + "	 Tname: " + tname +
    37 //                            "	 sex: " + sex + "	 score: " + score);
    38                 name = rs.getString(1);
    39                 System.out.println(name);
    40             }
    41             
    42         System.out.println("--------------------------------------");
    43     }
    44 };
    View Code

    Java连接Mysql数据库

     1 package cn.mldn.lxh ;
     2 import java.sql.Connection ;
     3 import java.sql.DriverManager ;
     4 public class DatabaseConnection {
     5     public static final String DBDRIVER= "com.mysql.jdbc.Driver";
     6     public static final String DBURL= "jdbc:mysql://localhost:3306/RUNOOB";
     7     public static final String DBUSER= "test";
     8     public static final String DBPASSWORD= "Changeme_123";
     9     
    10     private Connection conn ;
    11     public DatabaseConnection() throws Exception {
    12         Class.forName(DBDRIVER) ;
    13         this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
    14     }
    15     public Connection getConnection(){
    16         return this.conn ;
    17     }
    18     public void close() throws Exception {
    19         if(this.conn != null){
    20             try{
    21                 this.conn.close() ;
    22             }catch(Exception e){
    23                 throw e ;
    24             }
    25         }
    26     }
    27 }
    View Code

    jar包下载

    [更多参考]   

    http://www.runoob.com/java/java-mysql-connect.html

    Java实例---简单的数据库操作

    数据库建模工具 PD的使用

    Linu下的Mysql学习详解_【all】

  • 相关阅读:
    为什么要用getBaseContext()方法代替this?(转)
    如何让EditText不能自动获取焦点(转)
    context和getApplicationContext()的区别
    Idea 破解
    mysql 免安装
    AngularJS
    GC垃圾回收机制
    JVM类加载机制
    线程池
    面试-数据库
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9351745.html
Copyright © 2011-2022 走看看