JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 SQL 連接問題
KLYC





發文: 1
積分: 0
於 2017-01-11 10:51 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我第一個程式碼:
package YC;
import java.sql.*;

import YC.JAccess;
public class CreateTable {
   public static void main(String[] args) {
    JAccess.connect("test");
   String SQL="CREATE TABLE users(name VARCHAR(50),age SMALLINT,gender VARCHAR(5)," +
   "email VARCHAR(255),password VARCHAR(30))";
   JAccess.run(SQL);
  
   for (int i = 1 ; i<= 10000;i++){
     try{
       String AD = "'A" + i + "'";
     SQL = "insert into users(name,age,gender,email,password) " +
             "Values(" + AD + ",18" + ",'女'," + "'','300')" ;
     JAccess.run(SQL);
     
     }catch (Exception e){
       
     }
     
     
   }
  
   System.out.print("成功");
   JAccess.disconnect();
   }
}

第二個檔案:

package YC;
import java.sql.*;
import YC.JAccess;
public class Insert {
   public static void main(String[] args) {
    JAccess.connect("test");
    String SQL="INSERT INTO users(name,age,gender,email,password) " +
       "VALUES('紅紅','10','女','RedRed@gmail.com','123')";
    JAccess.run(SQL);
    SQL="INSERT INTO users(name,age,gender,email,password) " +
       "VALUES('綠綠',10,'女','GreenGreen@gmail.com','456')";
    JAccess.run(SQL);
    
    JAccess.disconnect();
    }
}

為什麼都第一個才能執行,我試著把第一個的code貼到第二個去,修改裡面的SQL語法這樣也是不行都會出現
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=test;user=Austone;password=1dayisgood;
java.lang.NullPointerException
java.lang.NullPointerException
java.lang.NullPointerException

我JAccess.Java的code:
package YC;
import java.sql.*;
public class JAccess {
static Connection conn;
static Statement stat;
public static void connect(String database) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dsn="jdbc:sqlserver://localhost;"+ "databaseName=" + database + ";user=Austone;password=1dayisgood;";
conn=DriverManager.getConnection(dsn); //帳號與密碼均為空字串 (未設)
stat=conn.createStatement(); //取得 Statement 物件
}
catch (Exception e) {System.out.printlnEnvelope;}
}
public static void disconnect() {
try {conn.close();}
catch (Exception e) {System.out.printlnEnvelope;}
conn=null;
stat=null;
}
public static ResultSet run(String SQL) {
ResultSet rs=null;
try {
if (SQL.indexOf("SELECT") == -1) {
       stat.executeUpdate(SQL);
       System.out.println(stat.executeQuery(SQL));
   }else{
     rs=stat.executeQuery(SQL);
   }
}
catch (Exception e) {System.out.printlnEnvelope;}
return rs;
}
}
真的想破頭不知道為什麼懇請大大們點解....

我是Java新手,請鞭小力一點


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
389 SQL 連接問題 KLYC 2656 2017-01-11 10:51
331 Re:SQL 連接問題 roytsang 380 2017-01-12 02:46
» JWorld@TW »  JDBC/SQL討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8