Java学习笔记-JDBC 1 博客分类: Java学习笔记 JDBCJavaMySQLSQLPostgreSQL
程序员文章站
2024-03-01 20:08:10
...
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.Driver jdbc.url=jdbc:mysql:///javacore jdbc.username=root jdbc.password=mysql
推荐阅读
-
Java学习笔记-JDBC 3 博客分类: Java学习笔记 JDBCJavaSQL
-
Java学习笔记-JDBC 1 博客分类: Java学习笔记 JDBCJavaMySQLSQLPostgreSQL
-
Java学习笔记-Class Loaders 博客分类: Java学习笔记 JavaEXTCC++C#
-
Java学习笔记-Class Loaders 博客分类: Java学习笔记 JavaEXTCC++C#
-
ActiveMQ学习笔记之四--启动嵌入式Broker(纯代码方式) 博客分类: java
-
学习JdbcProxy应用 博客分类: Java 应用服务器JDBC软件测试SQLMySQL
-
学习JdbcProxy应用 博客分类: Java 应用服务器JDBC软件测试SQLMySQL
-
JVM学习(1)-JVM运行时数据区 博客分类: java虚拟机 java虚拟机javajvm
-
Java概述与环境配置[Java 学习笔记 1]
-
Effective Java读书笔记、感悟——1.创建和销毁对象 博客分类: Java_SE effectivejava对象创建对象销毁