六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 167|回复: 0

Java学习笔记-JDBC 1

[复制链接]

升级  86%

9

主题

9

主题

9

主题

童生

Rank: 1

积分
43
 楼主| 发表于 2013-1-30 01:33:58 | 显示全部楼层 |阅读模式
Registering the Driver Class
 
There are 3 ways to register a driver to your java program.
 
1 A JAR file can be automatically register if it contains a file name META-INF/services/java.sql.Driver
 
2 Load the driver class in your java program. For example:
Class.forName("oracle.jdbc.OracleDriver"); 
3 Set jdbc.driver property. You can specify the property with a command-line argument, such as
java -Djdbc.drivers=org.postgresql.Driver ProgramName   Or your application can set the system property with a call such as
System.setProperty("jdbc.drivers", "org.postgresql.Driver"); 
Connecting to the Database

In your Java program, you open a database connection with code that is similar to the following example:
 
String url = "jdbc:postgresql:COREJAVA";String username = "dbuser";String password = "secret";Connection conn = DriverManager.getConnection(url, username, password); 
Following code is a whole example.
 
import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class DBTest {    public static void main(String[] args) {                try {            runTest();        } catch (SQLException ex) {            for (Throwable t : ex) {                t.printStackTrace();            }        } catch (IOException ex) {            ex.printStackTrace();        }    }        public static void runTest() throws IOException, SQLException {                Connection conn = getConnection();                try {            Statement stat = conn.createStatement();                        stat.executeUpdate("CREATE TABLE Greetings (Message CHAR(20))");            stat.execute("INSERT INTO Greetings VALUES ('Hello, World!')");                        ResultSet result = stat.executeQuery("SELECT * FROM Greetings");                        if (result.next()) {                System.out.println(result.getString(1));            }                        result.close();            stat.executeUpdate("DROP TABLE Greetings");                    } finally {            conn.close();        }    }        public static Connection getConnection() throws IOException, SQLException {                Properties props = new Properties();        InputStream in = DBTest.class.getResourceAsStream("database.properties");        props.load(in);                String drivers = props.getProperty("jdbc.drivers");        if (drivers != null) {            System.setProperty("jdbc.drivers", drivers);        } else {            return null;        }                String url = props.getProperty("jdbc.url");        String username = props.getProperty("jdbc.username");        String password = props.getProperty("jdbc.password");                    return DriverManager.getConnection(url, username, password);    }} 
database.properties file
 
jdbc.drivers=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:///javacorejdbc.username=rootjdbc.password=mysql  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表