java連線mysql資料庫 2 詳細

2021-09-12 01:16:47 字數 842 閱讀 6385

由statement引申而來的還有乙個preparedstatement類,一般在學習中使用preparedstatement較多,因為使用statement會產生sql注入

preparedstatement的使用方法 :preparestatement 語句物件名=連線物件名.preparestatement(sql語句);

舉個例子:

string sql="insert  into  vip(name,password)values (?,?)";//向表中插入資料,?就是佔位符

preparedstatement ps=con.preparestatement(sql)

ps.setstring(1,"zhangsan");//第乙個問號的資料就是zhangsan

ps.setstring(2,"123");  //第二個問號的資料是123

如果表中這一列的型別是int,那麼就   ps.setint(1,123);

建立時:

preparedment ps=con.preparedment(sql);

statement ps=con.createstatement();

執行時:

preparedstatement直接    物件名.excutequery();  或者物件名.excuteupdate();

statement                          物件名.excutequery("sql語句");  或者物件名.excuteupdate("sql語句")

Java連線MySQL資料庫

廢話不多說,直接上 記得在用eclipse建立專案後,要匯入jdbc。首先建立乙個databaseconnection類,用來連線資料庫。public class databaseconnection catch exception e public connection getconnectin ...

JAVA實現mysql資料庫連線

實現資料庫連線 功能 獲取乙個資料庫的連線,public static connection getconnection catch classnotfoundexception e catch sqlexception e return conn 注意 public static void mai...

java 連線到遠端mysql資料庫

首先載入mm.mysql2.0.4 bin.jar驅動,並把其加入classpath,注意一定要開啟遠端mysql的你的主機的訪問許可權 然後編寫如下 string name gjt.mm.mysql.driver 載入jdbc driver string url jdbc mysql 172.20...