java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
0. install mysql , and change the root password to root
1. download jdbc from mysql connector. or use this attachment.
2. tar -zxvf mysql-connector-java-5.1.23.
3. # export CLASSPATH=/usr/local/mysql-
4. vi getmySQLConnection.java // Try a simple //
import java.sql.*;
public class getmySQLConnection
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:mysql://localhost:3306/
}
}
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
Class.forName("com.mysql.jdbc.
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);
System.out.println("connected"
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
5. javac getmySQLConnection.java
6. java getmySQLConnection
7. A dbcmysql Engineer
dbcmysql.java
//package db;
import java.sql.*;
public class jdbcmysql {
private Connection con = null; //Database objects
private Statement stat = null;
private ResultSet rs = null;
private PreparedStatement pst = null;
private String dropdbSQL = "DROP TABLE User ";
private String createdbSQL = "CREATE TABLE User (" +
" id INTEGER " +
" , name VARCHAR(20) " +
" , passwd VARCHAR(20))";
private String insertdbSQL = "insert into User(id,name,passwd) " +
"select ifNULL(max(id),0)+1,?,? FROM User";
private String selectSQL = "select * from User ";
public jdbcmysql() // java-mysql-connect
{
try {
Class.forName("com.mysql.jdbc.
con = DriverManager.getConnection(
"jdbc:mysql://localhost/test?
"root","root");
}
catch(ClassNotFoundException e)
{
System.out.println("
}
catch(SQLException x) {
System.out.println("Exception :"+x.toString());
}
}
public void createTable() // java-mysql-create
{
try
{
stat = con.createStatement();
stat.executeUpdate(
}
catch(SQLException e)
{
System.out.println("CreateDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void insertTable( String name,String passwd)
{
try
{
pst = con.prepareStatement(
pst.setString(1, name);
pst.setString(2, passwd);
pst.executeUpdate();
}
catch(SQLException e)
{
System.out.println("InsertDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void dropTable() // java-mysql-drop
{
try
{
stat = con.createStatement();
stat.executeUpdate(dropdbSQL);
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
public void SelectTable() // java-mysql-select
{
try
{
stat = con.createStatement();
rs = stat.executeQuery(selectSQL);
System.out.println("ID\t\
while(rs.next())
{
System.out.println(rs.getInt("
rs.getString("name")+"\t\t"+
}
}
catch(SQLException e)
{
System.out.println("DropDB Exception :" + e.toString());
}
finally
{
Close();
}
}
private void Close() java-mysql-close
{
try
{
if(rs!=null)
{
rs.close();
rs = null;
}
if(stat!=null)
{
stat.close();
stat = null;
}
if(pst!=null)
{
pst.close();
pst = null;
}
}
catch(SQLException e)
{
System.out.println("Close Exception :" + e.toString());
}
}
public static void main(String[] args)
{
jdbcmysql test = new jdbcmysql();
test.dropTable();
test.createTable();
test.insertTable("yku", "12356");
test.insertTable("yku2", "7890");
test.SelectTable();
}
}
沒有留言:
張貼留言