1 package com.xiaoma.JDBC; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.util.Properties; 8 9 public class JDBC_PreparedStatement { 10 public static void main(String[] args) throws Exception{ 11 Connection conn=null; 12 PreparedStatement PS=null; 13 //讀取組態檔 14 InputStream is=JDBC_PreparedStatement.class.getClassLoader().getResourceAsStream("db.properties"); 15 Properties pro=new Properties(); 16 pro.load(is); 17 String Url=pro.getProperty("url"); 18 String UserName= pro.getProperty("username"); 19 String Pass= pro.getProperty("password"); 20 21 //注冊驅動 22 com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver(); 23 24 //獲取資料庫連接物件 25 conn= DriverManager.getConnection(Url,UserName,Pass); 26 27 //獲取預編譯的資料庫操作物件(洗掉和修改操作同理,只要吧值弄成問號即可,然后給問好傳值) 28 String InsertSQL="insert into t_user(login_name,login_pass,real_name) values(?,?,?)"; 29 PS=conn.prepareStatement(InsertSQL); 30 31 //給問號傳值 32 PS.setString(1,"user"); 33 PS.setString(2,"12345"); 34 PS.setString(3,"小馬"); 35 36 //執行SQL陳述句 37 int count=PS.executeUpdate(); 38 System.out.println(count); 39 40 //關閉連接 41 if (conn == null) { 42 conn.close(); 43 } 44 if (PS == null) { 45 PS.close(); 46 } 47 } 48 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/309006.html
標籤:其他
下一篇:JAVA 考試系統模塊設計方案
