Java JDBC接続と使用
JDBC
ドライバーをインポートします
// JARはパッケージ化されたクラスファイルセットであり、他のプロジェクトを参照できます
//ビルドパスで外部ジャーをインポートします
JDBCに接続します
1。ドライバーをロードします
class.from( "com.mysql.jdbc.driver");
接続を作成します
//ガイドパッケージはjava.sql。*を使用します。 string jdbc = "jdbc:mysql:// localhost:3306/sudine?user = root&password =&charaterencoding = utf-8"; //生徒はテーブル名conn = drivermanager.getConnection(jdbc);
2。データベースが開いた後、閉じることを忘れないでください。
conn.close();
1。SQLステートメントを実行します(テーブルの作成、挿入、削除、更新)
統計者を使用します
statemant st = conn.createstatemant(); int row = St.ExeCuteUpDate(SQLステートメント); //クエリ操作を実行できません。
準備を使用しています
使用できますか?渡す必要があるパラメーターを置き換えるプレースホルダー
string sql = "inserting" + tablename + "(name、subject、score)values(?、?、?)"; preperestatement pt = conn.preparestatement(sql); //各プレースホルダーの値を設定すると、添え字は1 pt.setString(1、score.getName())から始まります。 pt.setString(2.score.getSubject()); pt.setDouble(3、score.getScore()); //パラメーターなしでメソッドを使用しますpt.executeUpdate();
1。クエリ操作
静的リスト<Score> QueryScore(Connection PConn、Score PSCORE)スローSQLEXCEPTION {ARRAYLIST <SCORE> MLIST = new ArrayList <>(); string sql = "select * from" + tablename + "where name =?"; preatedStatement ps = pconn.preparestatement(sql); ps.setString(1、pscore.getName());結果rs = ps.executequery(); while(rs.next()){//ここでは、rs string subject = rs.getString( "subject")を介してすべての結果を取得できます。 int id = rs.getint( "id");ダブルスコア= rs.getDouble( "スコア"); mlist.add(new Score(id、pscore.getname()、subject、score)); } mlistを返します。 }これが小さなプログラムです
//データベース接続クラスの作成パブリッククラスdao {//データベースリンクアドレスを尋ねますstatic string jdbc = "jdbc:mysql:// localhost:3306/sudine?user = root&password =&charatereCoding = utf-8"; //リンクを開きpublic static connection(){// JDBCを使用する手順//1。JDBCドライバーをロードしてください{//クラスのフルネーム +クラス名class.forname( "com.mysql.jdbc.driver"); //2。データベース接続に接続するconn = drivermanager.getConnection(jdbc); conn; } catch(Exception e){System.out.println( "ドライバーロード障害"); nullを返します。 }}} //スコアクラスパブリッククラススコア{string name;文字列ID;文字列件名;ダブルスコア;パブリックスコア(文字列名、文字列件名、ダブルスコア){super(); this.name = name; this.subject = subject; this.score = score; } @Override public string toString(){return "score [name =" + name + "、id =" + id + "、subject =" + subject + "、score =" + score + "]"; } public score(string name、string id、string subject、double score){super(); this.name = name; this.id = id; this.subject = subject; this.score = score; } public string getname(){return name; } public void setName(string name){this.name = name; } public string getId(){return id; } public void setid(string id){this.id = id; } public string getSubject(){return subject; } public void setSubject(string subject){this.subject = subject; } public double getScore(){return score; } public void setScore(double score){this.score = score; }} //実装クラスパブリッククラステスト{public static string tablename = "score"; public static void main(string [] args){try {connection conn = dao.connection(); if(conn!= null){system.out.println( "linked"); // createTable(conn); //レコードを挿入//スコアスコア=新しいスコア( "li si"、 "android"、98); // system.out.println(addscore2(conn、score)); // deletescore(conn、score); // updateScore(conn、score);リスト<スコア> list = queryscorebyname(conn、 "wang wu"); // queryallscore(conn); for(スコアスコア:list){System.out.println(score); } conn.close(); } else {system.out.println( "link failed"); }} catch(sqlexception e){e.printstacktrace(); }} //テーブルの作成public static boolean createTable(Connection Conn){// SQLステートメントString sql = "Create Table" + TableName + "(id Integer Primary Key Auto_increment、Name varchar(3)Not null、subject varchar(20)not not null、score double)"; //ステートメントを実行するには、実行されたクラスを実行する必要がありますステートメントtry {ステートメントst = conn.createstatement(); int result = St.ExeCuteUpDate(SQL); system.out.println(result); if(result!= -1)trueを返します。 } catch(sqlexception e){e.printstacktrace(); } falseを返します。 } //レコードを追加するpublic static boolean addscore(接続conn、スコアスコア)sqlexception {string sql = " + tablename +"(name、sublename + "( '" + score.getname() + "'、 '" + score.getsubject() + "'、" + scorsscore( " + score() +") ") System.out.println(SQL);ステートメントst = conn.createstatement(); int row = St.ExeCuteUpDate(SQL); if(row> 0)trueを返します。 falseを返します。 } //レコードの追加string sql = "inserting" + tablename + "(name、subject、score)values(?、?、?)"(?、?)に設定する必要があるパラメーターを置き換えるには。 preatedStatement ps = conn.preparestatement(SQL); //与えられなければなりませんか? ps.setString(1、score.getName())によって表される値; ps.setString(2、score.getSubject()); ps.setdouble(3、score.getscore()); //パラメーターなしでメソッドを呼び出すint row = ps.executeupdate(); if(row> 0)trueを返します。 falseを返します。 } public static boolean deletescore(connection conn、スコアスコア)はsqlexceptionをスローします{string sql = "delete from" + tablename + "where name =?and subject =?"; // prepareStatement predictatement PS = conn.Preparestatement(SQL)を作成します。 ps.setString(1、score.getName()); ps.setString(2、score.getSubject()); // ps.setDouble(3、score.getScore()); // int row = ps.executeupdate(); System.out.println(row); if(row> 0)trueを返します。 falseを返します。 } public static boolean updatescore(connection conn、score score)throws sqlexception {//件名のstring sql = "update" + tablename + "set score =?where name =?and subject =?"; preatedStatement ps = conn.preparestatement(SQL); ps.setdouble(1、score.getscore()); ps.setString(2、score.getName()); ps.setString(3、score.getSubject()); int row = ps.executeupdate(); System.out.println(row); if(row> 0)trueを返します。 falseを返します。 } public static list <Score> queryallscore(connection conn)throws sqlexception {string sql = "select * from" + tableName; //クエリステートメントを開始st = conn.createstatement(); resultet rs = St.Executequery(SQL); List <Score> list = new ArrayList <Score>(); while(rs.next()){//ここでは、rs string id = rs.getString( "id")を介してすべての結果を取得できます。文字列名= rs.getString( "name");文字列件名= rs.getString( "subject");ダブルスコア= rs.getDouble( "スコア"); list.add(new Score(name、id、subject、score)); } //返品リストを終了します。 } public static list <Score> QueryScoreByName(Connection Conn、String name)throws sqlexception {string sql = "select * from" + tableName + "where name =?"; preatedStatement Pt = conn.Preparestatement(SQL); pt.setString(1、name);結果rs = pt.executequery();リスト<スコア> list = new ArrayList <>(); while(rs.next()){string subject = rs.getString( "subject");文字列id = rs.getString( "id");ダブルスコア= rs.getDouble( "スコア"); list.add(new Score(name、id、subject、score)); }返品リスト。 }