码迷,mamicode.com
首页 > 数据库 > 详细

Java学习---连接数据库操作

时间:2018-07-22 23:44:10      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:core   rgs   for   oracle数据库   connect   tst   nec   bpa   ftl   

 

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 + "\t Tname: " + tname +
37 //                            "\t sex: " + sex + "\t 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学习---连接数据库操作

标签:core   rgs   for   oracle数据库   connect   tst   nec   bpa   ftl   

原文地址:https://www.cnblogs.com/ftl1012/p/9351745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!